2

我正在开发一个 iOS 应用程序,并且有一个 UITableView 包含文本消息(每条消息的表格单元格)。当有新消息到达时,UITableView 中会添加一个新单元格。起初,我使用了一个 UITableView 部分并在其下添加了所有单元格。

一切都很好,直到我决定更改方案并使用多个部分,每个部分都有一行(这是我发现在单元格之间强制留出空间的最干净的方法)。这个方案的问题是,每次我尝试添加一个新单元格时,我都会遇到这个异常:

2015-09-30 11:48:35.659 restcomm-messenger[15069:489766] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UITableView.m:1702

我在执行 [self.tableView endUpdates] 时得到它(完整代码见下文)

我在类似的问题中检查了许多 SO 答案,但我认为我做的一切都是正确的。以下是“有趣”的代码部分:

新消息到达,更新后备存储并将行插入表:

...
// update the backing store
[self.messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:type, @"type", msg, @"text", nil]];

[self.tableView beginUpdates];
// trigger the new table row creation
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:[self.messages count] - 1]]
                      withRowAnimation:animation];
[self.tableView endUpdates];
...

节和行计数回调:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.messages count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

填充单元格回调:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *type = [[self.messages objectAtIndex:indexPath.section] objectForKey:@"type"];
    if ([type isEqualToString:@"local"]) {
        LocalMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"local-message-reuse-identifier" forIndexPath:indexPath];
        cell.senderText.text = [[self.messages objectAtIndex:indexPath.section] objectForKey:@"text"];
        return cell;
    }
    else {
        RemoteMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"remote-message-reuse-identifier" forIndexPath:indexPath];
        cell.senderText.text = [[self.messages objectAtIndex:indexPath.section] objectForKey:@"text"];
        return cell;
    }
}

我希望这能奏效。请记住,对于原始方案(1 节可能行),它工作得很好:(

如果您想了解更多详细信息,以下是相关表视图控制器的完整代码:https ://github.com/Mobicents/restcomm-ios-sdk/blob/master/Lab/restcomm-messenger-debug/restcomm-messenger/MessageTableViewController .m

欢迎任何提示

安东尼斯

4

0 回答 0