dequeueReusableCellWithIdentifier,自定义UITableViewCell问题

时间:2010-03-22 15:43:14

标签: iphone ios uitableview

我有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,则不会重新创建一个单元格,但是这样我就丢失了所有不可见的单元格信息。

有什么想法吗?

提前致谢

2 个答案:

答案 0 :(得分:4)

我遇到了类似的问题 - 我认为问题是在任何给定时间只有可见单元实际上在内存中,当它重新显示旧单元时,它实际上只是将一个新单元出列。我认为解决方案是使用- (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
    }
}

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

答案 1 :(得分:-1)

使用此:

IGECellBasic *cell = [[[IGECellBasic alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
相关问题