【问题标题】:Cell is not being rendered in UITableView after insert/delete cells插入/删除单元格后,单元格未在 UITableView 中呈现
【发布时间】:2013-01-03 04:57:42
【问题描述】:

我有一个可以在 2 种模式下工作的表格视图:只读和读写。在只读模式下,表格仅包含 4 个单元格,在读写模式下 - 8 个单元格。要在这 2 种模式之间转换,我有一个带有已实现操作的按钮:

...
NSArray* cellIndexesInCurrentMode = [self cellIndexesInMode:Mode1];
NSArray* cellIndexInNewMode = [self cellIndexesInMode:Mode2];

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths: cellIndexInNewMode withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:cellIndexesInCurrentMode withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

一切正常,直到我以读写方式向下滚动表格(8 个单元格),所以它反弹了。之后,点击按钮切换到 mode2 会导致其中一个单元格未渲染的情况:

                 before transition:  

                 after transition:

我的调查表明,由于某种原因,在 UITableView 上最后一次 layoutSubviews 调用之后,此单元格的 alpha = 0。

我被卡住了,我不知道发生了什么,所以任何帮助将不胜感激。

更新: 这是我的 cellForRowAtIndexPath 方法: - (UITableViewCell*) tableView :(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)cellForRowAtIndexPath { IDataSourceElement item = [[self collection] getDataSourceElementAtIndex:cellForRowAtIndexPath.row];

MyTableCell* cell = nil;

if ([item conformsToProtocol:@protocol(MyTableCellBuilder)]) {
    cell = [(MyTableCellBuilder)item tableView:tableView buildCellForIndexPath:cellForRowAtIndexPath];
} 

if (cell == nil) {
    Class cellClass = [self getCellClassForElement: item];

    NSString* reuseId = [cellClass description];

    cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell) {
        cell = [[[cellClass alloc] initWithReuseIdentifier :reuseId] autorelease];
    }
}


[self fillCell:cell forTableView:tableView forIndexPath:cellForRowAtIndexPath];

return cell;

}

和MyTableCellBuilder buildCellForIndexPath:方法实现

- (MyTableCell*) tableView :(UITableView*)tableView buildCellForIndexPath:(NSIndexPath*)buildCellForIndexPath {
    MyTableCell* cell = myTableCell;
    if ([cell isKindOfClass:[TextPropertyCell class]] == NO) {
        cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier :[self cellReuseIdentifier]];
        if ([cell isKindOfClass:[MyTableCell class]] == NO) {
            cell = [[[MyTableCell alloc] initWithReuseIdentifier :[self cellReuseIdentifier]] autorelease];
        }
        [self prepareCell:cell];
    }

        //cell setup
        ...
    ...
    return cell;
}

我的想法是缓存 UITableViewCell 实例的问题,我知道这不是最好的方法,但无论如何我有它。我试图删除缓存,所以我所做的只是将 myTableCell 设置为 nil,因此它永远不会从此方法返回。而且......它有帮助!我不知道为什么,很想知道答案?

【问题讨论】:

  • 当 alpha 设置为 0 时,它是隐藏/不可见的...设置 alpha 为 1.. 使其再次可见。
  • :) 谢谢,但我想知道为什么它首先设置为 0

标签: objective-c ios uitableview


【解决方案1】:

显示您的 cellForRowAtIndexPath 方法实现,我确定您应该在此处查找错误。

【讨论】:

  • cellForRowAtIndexPath 方法的实现很难展示,因为它非常复杂,我用类似于真实的代码更新了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多