OGeek|极客世界-中国程序员成长平台

标题: iphone - dequeueReusableCellWithIdentifier、自定义 UITableViewCell 的问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:41
标题: iphone - dequeueReusableCellWithIdentifier、自定义 UITableViewCell 的问题

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

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

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

有什么想法吗?

提前致谢



Best Answer-推荐答案


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

- (void)tableViewUITableView *)tableView willDisplayCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4