iOS 的9宫格实现方式有很多种,这里是参考了网上某某的demo后,几乎照着做了一遍。
这里我采用订制cell的方式来实现
这里给出几点注意事项:
1,订制的cell里面的重用标识符一定要和代码中的标识符一样。不然重用机制不能生效
#pragma mark
#pragma mark -UITalbeViewCellDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"ViewCell";
DH_Cell *cell = (DH_Cell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"DH-Cell" owner:self options:nil] objectAtIndex:0];
// [[NSBundle mainBundle] loadNibNamed:@"DH-Cell" owner:self options:nil];
// cell = viewCell;
cell.delegate = self;
}
//取消行选中色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ((indexPath.row*3)<=([mItemArray count]-1))
{
CellObject * i = [mItemArray objectAtIndex:indexPath.row*3];
cell.lab1.text = [NSString stringWithFormat:@"%@",i.Title];
[cell.btn1 setImage:[UIImage imageNamed: [NSString stringWithFormat:@"%@",i.Image]] forState:UIControlStateNormal];
//nib cell中没有设置tag,这里设置tag。
[cell.btn1 setTag:indexPath.row*3];
//单个隐藏第2.第3
if (indexPath.row == mItemArray.count/3) {
cell.btn2.hidden = YES;
cell.btn3.hidden = YES;
cell.lab2.hidden = YES;
cell.lab3.hidden = YES;
}
}
if ((indexPath.row*3+1)<=([mItemArray count]-1))
{
CellObject * i = [mItemArray objectAtIndex:indexPath.row*3+1];
cell.lab2.text = [NSString stringWithFormat:@"%@",i.Title];
[cell.btn2 setImage:[UIImage imageNamed: [NSString stringWithFormat:@"%@",i.Image]] forState:UIControlStateNormal];
[cell.btn2 setTag:indexPath.row*3+1];
if (indexPath.row == mItemArray.count/3) {
cell.btn3.hidden = YES;
cell.lab3.hidden = YES;
}
}
if ((indexPath.row*3+2)<=([mItemArray count]-1))
{
CellObject * i = [mItemArray objectAtIndex:indexPath.row*3+2];
cell.lab3.text = [NSString stringWithFormat:@"%@",i.Title];
[cell.btn3 setImage:[UIImage imageNamed: [NSString stringWithFormat:@"%@",i.Image]] forState:UIControlStateNormal];
[cell.btn3 setTag:indexPath.row*3+2];
}
return cell;
}
代码我也一并附上,代码里面关键地方有注释,给初学的同学看看。
demo地址:
http://pan.baidu.com/share/link?shareid=434473&uk=2315407450