草庐IT

iphone - dequeueReusableCellWithIdentifier 问题,自定义 UITableViewCell

coder 2024-01-17 原文

我有带 2 个按钮的自定义单元格(这些按钮的功能只是禁用按下的按钮)。 当我以这种经典方式使用 dequeueReusableCellWithIdentifier 时:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = ((MainCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]);
 if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:self options:nil];
 }
    return cell;
}

UITableView 有一个部分,问题是:在第一个单元格上,当我按下按钮将其禁用,然后向下滚动以显示其他单元格时,当我再次向上滚动时,第一个单元格是一个新单元格,按钮是启用。 我知道 reuseIdentifier 用于不重新创建已经创建的单元格,但通过这种方式我丢失了所有不可见的单元格信息。

有什么想法吗?

提前致谢

最佳答案

我遇到了类似的问题——我认为问题在于在任何给定时间实际上只有可见单元格在内存中,当它重新显示一个旧单元格时,它实际上只是使一个新单元格出队。我认为解决方案是使用 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 委托(delegate)方法:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    id objectForCell = [self.arrayOfThingsForTableView objectAtIndex:indexPath.row];
    if (!objectForCell.button1IsEnabled) { 
        cell.button1.enabled = NO; //or something along those lines
    } else {
        cell.button1.enabled = YES; //necessary so that all the other buttons don't disable
    }
}

如果有人有更好的解决方案,我会很高兴听到。

关于iphone - dequeueReusableCellWithIdentifier 问题,自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2493598/

有关iphone - dequeueReusableCellWithIdentifier 问题,自定义 UITableViewCell的更多相关文章

随机推荐