ios - UITableView 在顶部插入行崩溃

标签 ios uitableview

我试图在 UITableView 的顶部插入一行,但它在我声明 endUpdating 的最后一行崩溃了。这是我正在使用的代码,我正在使用表格外的按钮来插入行。

[comments insertObject:comment atIndex:0];
[self.commentsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.commentsTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.commentsTableView endUpdates]; //here crashes with "assertion failure"

我知道我可以只插入数组并重新加载,但我想用动画来完成。 提前谢谢你。

这是我的行方法单元格

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CommentCell";
// Similar to UITableViewCell, but
ListingCommentTableViewCell *cell = (ListingCommentTableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[ListingCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.commenterLabel.text = [[comments objectAtIndex:indexPath.row] objectForKey:@"user"];
[cell.commenterLabel sizeToFit];
cell.commentLabel.text = [[comments objectAtIndex:indexPath.row] objectForKey:@"text"];
[cell.commentLabel sizeToFit];
cell.lineView.frame = CGRectMake(5 , cell.commentLabel.frame.origin.y + cell.commentLabel.frame.size.height+ 5, cell.frame.size.width-20, 1);
cell.dateLabel.text = [Utils formatCommentDate:[[comments objectAtIndex:indexPath.row] objectForKey:@"createdOn"]];
return cell;

这是我的单元格高度方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

int height = [Utils findHeightForText:[[comments objectAtIndex:indexPath.row] objectForKey:@"teksti"] havingWidth:screenWidth - 40   andFont:[UIFont fontWithName:@"Ubuntu" size:14]];

height += 25;

return height + 5;
}

和我的行数方法

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return [comments count];
}

最佳答案

这就是我使用动画将对象插入 UITableView 的方式。

-(void)updateMethod {
    [self.commentsTableView beginUpdates];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:0];
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [comments insertObjects:[NSArray arrayWithObjects:comment, nil] atIndexes:indexSet];
    [self.commentsTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.commentsTableView endUpdates]; 
}

您应该检查 numberOfRowsInSection 并确保它反射(reflect)您的 comments.count

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return comments.count;
}

此外,所有 UI 更新都必须在主线程上完成,如果您在不同的线程中调用您的更新方法,则发生更改时您会看到该错误。

关于ios - UITableView 在顶部插入行崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24866467/

相关文章:

ios - UISplitViewController 在 Storyboard中隐藏/取消隐藏 MasterView

swift - 如何在tableView中调整单元格大小

python - 从Windows将python脚本转换为dmg程序

iphone - Sqlite 不执行查询 Some time

ios - iOS 中 UITableViewCell 中的 setHighlighted 和 setSelected 有什么区别?

ios - 点击单元格时从 uitableviewcell 类调用 uipangesture

ios - 如何让按钮在所有尺寸的单元格中都位于右侧?

ios - Alpha 在 UITableViewCell 中不起作用

ios - 在 cellForRowAt 中无法使用 UITableView 但在 Swift 中可以使用 numberOfRowsInsection

ios - MPMusicPlayerController不控制iOS 7中的Podcast应用