关于九宫格

关于九宫格形式:

1.UITableView

利用自定义的UITableViewCell形式,注意tableview的重用机制

2.UICollectionView

同样可以自定义CollectionCell,但是CollectionCell本身就是一个Item,不想UITableViewCell本身包含多个Item,注意CollectionHeader和CollectionViewLayout

[self.productsView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];

[self.productsView registerClass:[CollectionHeader class] forSupplementaryViewOfKind:@"CollectionHeader" withReuseIdentifier:@"CollectionHeader"];

CollectionViewLayout *layout = [[CollectionViewLayout alloc] init];

[self.productsView setCollectionViewLayout:layout];


3.利用UIScrollView和UITableView

(1)设置基本属性

NSInteger count = ([self.dataArray count]-1)/8+1;

        

self.myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 74, 804, 400)];

self.myScrollView.delegate = self;

self.myScrollView.contentSize = CGSizeMake(804*count, self.myScrollView.frame.size.height);

self.myScrollView.pagingEnabled = YES;

self.myScrollView.showsVerticalScrollIndicator = NO;

        self.myScrollView.showsHorizontalScrollIndicator = NO;

[self.stepView_0 addSubview:self.myScrollView];

        

for (int i=0; i<count; i++) {

       self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0+804*i, 0, 804, self.myScrollView.frame.size.height)];

        self.myTable.tag = i;

        self.myTable.delegate = self;

        self.myTable.dataSource = self;

        self.myTable.scrollEnabled = NO;

        self.myTable.backgroundColor = [UIColor clearColor];

        [self.myScrollView addSubview:self.myTable];

  }


CGRect frame = [self.stepView_0 bounds];

frame.origin.y = 0;

frame.origin.x = 0;

[self.myScrollView setContentOffset:CGPointMake(frame.origin.x, frame.origin.y)];

    }

(2)在tableview代理里面实现布局

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger count = ([self.dataArray count]-1)/8+1;

    

    static NSString *CellIdentifier = @"Cell";

    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        for (int i = 0; i<count; i++) {

            if (tableView.tag==i) {

                [self drawTableViewCell:cell index:[indexPath row] category:i];

            }

        }

}

    

    return cell;

}

(3)调用drawTableViewCell:cell

//绘制tableviewcell

-(void)drawTableViewCell:(UITableViewCell *)cell index:(int)row category:(int)category{

    int maxIndex = (row*4+3);

    int number = [self.dataArray count]-8*category;

if(maxIndex < number) {

for (int i=0; i<4; i++) {

[self displayPhotoes:cell row:row col:i category:category];

}

return;

}

else if(maxIndex-1 < number) {

for (int i=0; i<3; i++) {

[self displayPhotoes:cell row:row col:i category:category];

}

return;

}

else if(maxIndex-2 < number) {

for (int i=0; i<2; i++) {

[self displayPhotoes:cell row:row col:i category:category];

}

return;

}

else if(maxIndex-3 < number) {

[self displayPhotoes:cell row:row col:0 category:category];

return;

}

}

(4)Item的实现

-(void)displayPhotoes:(UITableViewCell *)cell row:(int)row col:(int)col category:(int)category

{

    NSInteger currentTag = 4*row+col+category*8;

    ..................

    ..................

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值