ios中表格单元格的可重用问题

问题描述:

我在表格视图中为所有行添加了2个按钮,并且这些按钮首次出现在表格视图中,当我滚动表格列表中的按钮时,点击禁用, 这里是我的代码ios中表格单元格的可重用问题

- (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; 
} 

这是因为发生的事情,每次都是滚动的tableview时间,你的细胞被再利用,在这种情况下,细胞不是零和cell==nil之前的代码上面的代码,使得userInteractionEnabled到NO。这就是为什么,你的按钮不可点击。

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

+0

我尝试userintercationEnable =没有,但它不是wrkin – user2256034

+0

@ user2256034使'userInteractionEnabled = YES',它将工作 –

+0

其干活感谢名单 – user2256034