ios-实现9宫格

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值