自定义TableViewCell上的按钮-找到cell行数

这个类继承于

UITableViewCell

开始的时候我不会写方法和传递参数

就全部写在

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier里面

但是后来就不太对了,他不能保存从tableView里面传过来的行数

  UIButton * roomBtn =[UIButton buttonWithType:UIButtonTypeCustom];

 for (int i=0; i<7; i++)

    {

      UIButton * roomBtn =[UIButton buttonWithType:UIButtonTypeCustom];

        

        tag =row*7+i;

        roomBtn.frame = CGRectMake(i*120+50,20, 96, 96);

        [roomBtn setTag:tag];

        [roomBtn setTitle:[arrayobjectAtIndex:tag] forState:UIControlStateNormal];

        [self.contentViewaddSubview:roomBtn];

        self.selectionStyle =NO;

        roomBtn.backgroundColor = [UIColorcolorWithPatternImage: [UIImageimageNamed:@"free"]];

        [roomBtn addTarget:selfaction:@selector(TapRowBtn:)forControlEvents:UIControlEventTouchUpInside];

        [roomBtn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

       

    }

  然后执行出来就只有一个按钮,看了半天不知道为啥,自认为位置没有写错,但是所有按钮都指向了同一个位置

千万要注意,按钮的初始化要卸载for循环里面

  按钮的初始化要卸载for循环里面

按钮的初始化要卸载for循环里面

因为我需要7个按钮,而不是1个

后来老师教了方法

自己写一个

-(void)customlizedWithData:(id)data findAllname:(NSArray *)array


把tableVIew的行数传到tableViewcell里面

注意

注意

-(ZMCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier =@"cellID";

   ZMCell * cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    //判断cell是否为空

    if (cell == nil)

    {

        cell = [[ZMCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    }

//    cell重新赋值

    [cell customlizedWithData:[NSNumber numberWithInt:indexPath.row] findAllname:roomsNames];

在zmcell.m里面

//重新赋值

-(void)customlizedWithData:(id)data findAllname:(NSArray *)array


{

    

   NSNumber *line = data;

    row = [line intValue];

   NSLog(@"%d",row);

   int tag;

   BOOL isLastRow = (row +1) * 7 >= array.count;

   int roomNum = 0;

    //    不是最后一行

   if (!isLastRow || array.count %7 == 0) {

        roomNum =7;

    }else {

       //是最后一行

        roomNum = array.count %7;

    }


   for (int i=0; i<roomNum; i++)

    {

        UIButton * roomBtn =[UIButtonbuttonWithType:UIButtonTypeCustom];

        

        tag =row*7+i;

        roomBtn.frame =CGRectMake(i*120+50,20, 96, 96);

        [roomBtnsetTag:tag];

        [roomBtn setTitle:[arrayobjectAtIndex:tag] forState:UIControlStateNormal];

        [self.contentViewaddSubview:roomBtn];

        self.selectionStyle =NO;

        roomBtn.backgroundColor = [UIColorcolorWithPatternImage: [UIImageimageNamed:@"free"]];

        [roomBtn addTarget:selfaction:@selector(TapRowBtn:)forControlEvents:UIControlEventTouchUpInside];

        [roomBtn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

       

    }

   接下来就是最重要的了

//重新初始化

- (void)prepareForReuse

{

    //自动识别cell是否可以重用

    [super prepareForReuse];

    // 隐藏原有按钮

    for (UIView *btnin self.contentView.subviews) {

        [btn removeFromSuperview];

    }

}

 如果没有

 for (UIView *btn in self.contentView.subviews) {

        [btn removeFromSuperview];

    }的话

每一行的按钮都会显示满

最后一行的余数也会显示满

最好的方法不是remove掉,而是隐藏掉

因为tableview的重用机制存在,

他每次remove的时候下次就要再继续生成,很浪费内存

所以应该用btn.hidden = yes;






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值