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

来源:互联网 发布:阿里巴巴大数据学院 编辑:程序博客网 时间:2024/05/22 14:27
这个类继承于

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 = [lineintValue];

   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 *btninself.contentView.subviews) {

        [btn removeFromSuperview];

    }

}

 如果没有

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

        [btn removeFromSuperview];

    }的话

每一行的按钮都会显示满

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

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

因为tableview的重用机制存在,

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

所以应该用btn.hidden = yes;