【问题标题】:Table cell reusebiliy issue in iosios中的表格单元可重用性问题
【发布时间】:2013-05-29 05:24:30
【问题描述】:

我在表格视图的一行中为所有行添加了 2 个按钮,这些按钮在第一次出现在表格视图中时被点击,当我滚动表格列表时,按钮点击禁用, 这是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"ImageOnRightCell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.userInteractionEnabled = NO;
     UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeCustom];
     UIButton *finalPriceBtn1=[UIButton buttonWithType:UIButtonTypeCustom];

     if (cell == nil)
     {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         int i=indexPath.row;

         finalPriceBtn.backgroundColor=[UIColor redColor];
         finalPriceBtn.tag=MAINLABEL_TAG;
         finalPriceBtn.frame = CGRectMake(200, 0.0, 100, 50);
         [finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
         finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];

         finalPriceBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
         [finalPriceBtn setImage:[UIImage imageNamed:@"man.jpg"] forState:UIControlStateNormal ];
         [cell.contentView addSubview:finalPriceBtn];

         finalPriceBtn1.backgroundColor=[UIColor redColor];
         finalPriceBtn1.tag=SECONDLABEL_TAG;
         finalPriceBtn1.frame = CGRectMake(50.0, 0.0, 80.0, 45.0);
         [finalPriceBtn1 addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
         finalPriceBtn1.titleLabel.font=[UIFont systemFontOfSize:12];

         finalPriceBtn1.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
         [finalPriceBtn1 setImage:[UIImage imageNamed:@"bulk-female.jpg"] forState:UIControlStateNormal ];
         [cell.contentView addSubview:finalPriceBtn1];
     }
     else
     {
         finalPriceBtn = (UIButton *)[cell.contentView viewWithTag:MAINLABEL_TAG];
         finalPriceBtn1 = (UIButton *)[cell.contentView viewWithTag:SECONDLABEL_TAG];

     }
     return cell;
}

【问题讨论】:

    标签: ios uitableview cell reusability


    【解决方案1】:

    发生这种情况是因为,每次您滚动 tableview 时,您的单元格都会被重用,在这种情况下,单元格不是 nil,并且cell==nil 之前的代码上方的代码会使 userInteractionEnabled 变为 NO。这就是为什么您的按钮不可点击的原因。

    第一次这些按钮是可点击的,因为它们没有被分配,我的意思是单元格没有被分配,并且将任何属性设置为未分配的实体都没有效果。希望你明白了。

    【讨论】:

    • 我尝试 userintercationEnable=no,但它不是 wrkin
    • @user2256034 使userInteractionEnabled = YES,它会工作
    • 请告诉我 userInteractionEnabled = YES 背后的问题,为什么不呢
    • @user2256034 如果你让userInteractionEnabled = NO,UITableViewCell中添加的所有元素都会变成userInteractionEnabled = NO,这意味着它不会检测到任何点击!或任何手势。
    • 请帮助更多,我之前已经创建了核心数据,现在我必须在 ecxistin 表中添加更多实体,但我不想丢失我以前的数据,帮我拉詹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多