关于九宫格形式:
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
//绘制tableview的cell
-(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;
..................
..................
}