【问题标题】:Issue with dequeueReusableCellWithIdentifier, custom UITableViewCelldequeueReusableCellWithIdentifier、自定义 UITableViewCell 的问题
【发布时间】:2010-03-22 15:43:14
【问题描述】:

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

有什么想法吗?

提前致谢

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      使用这个:

      IGECellBasic *cell = [[[IGECellBasic alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
      

      【讨论】:

      • 这只是我的 UITableViewCell 的子类
      猜你喜欢
      • 2016-03-15
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多