Ios UITableViewCell的怪异行为

Ios UITableViewCell的怪异行为,ios,objective-c,uitableview,Ios,Objective C,Uitableview,您好,我最近在UITableViewCell上遇到了一个问题 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ContentCell"; UITableViewCell *cell = [tableView dequeueReusableCellWi

您好,我最近在UITableViewCell上遇到了一个问题

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"ContentCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    CloseButton *cButton = (CloseButton *)[cell viewWithTag:20];
    [cButton addTarget:self action:@selector(deleteDetector:) forControlEvents:UIControlEventTouchUpInside];
     ...
return cell;
}
稍后,在我的删除检测器上:

-(void)deleteDetector:(id)sender {
    CloseButton *cButton = (CloseButton *)sender;

    [cButton setHidden:YES];
}

当我开始向下滚动到大约1000个单元格时,按钮开始出现,其中一些开始消失。

好的,如果我正确理解了你的问题,我假设发生了什么:

您正在按下单元格上的按钮,这会使按钮隐藏。然后,再向下滚动,另一个单元格显示,按钮已隐藏(即使尚未按下该行的按钮)

这是因为您的单元实际上正在被重用,这意味着当已经隐藏按钮的单元之一被重用时,该按钮仍将被隐藏(因为它实际上是同一个单元)。证明这一点的“快速修复”方法是取消隐藏
tableView:cellforrowatinexpath:
方法中的按钮,如下所示:

[cButton setHidden:NO];
在这之后的某个地方这样做,显然:

CloseButton *cButton = (CloseButton *)[cell viewWithTag:20];
这样可以防止单元格在不应该隐藏按钮的情况下出现。然而,这也意味着,如果你按下一个单元格上的按钮,然后它离开屏幕,然后又重新打开,它也会再次显示按钮,而你可能不希望它这样做。如果不想发生这种情况,您必须在模型中的某个位置跟踪您按下按钮的行