Ios UITableView在顶部插入行崩溃
我试图在Ios UITableView在顶部插入行崩溃,ios,uitableview,Ios,Uitableview,我试图在UITableView的顶部插入一行,但它在我声明endupdateing的最后一行崩溃。这是我正在使用的代码,我正在使用表外的按钮插入行 [comments insertObject:comment atIndex:0]; [self.commentsTableView beginUpdates]; NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.commentsTableVi
UITableView
的顶部插入一行,但它在我声明endupdateing
的最后一行崩溃。这是我正在使用的代码,我正在使用表外的按钮插入行
[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];
}
应更新动画块内的数据源阵列:
[self.commentsTableView beginUpdates];
[comments insertObject:comment atIndex:0];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.commentsTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.commentsTableView endUpdates];
这就是我如何通过动画将对象插入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];
}
您应该检查章节中的numberofrowsin
,并确保它反映了您的评论。count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return comments.count;
}
此外,所有UI更新都必须在主线程上完成,如果在不同的线程中调用更新方法,则会出现一个更改,您可以看到该错误。问题在于找出它崩溃的原因,或者如何设置动画?您得到的确切错误是什么?@Student-(void)insertRowsAtIndexPaths:(NSArray*)indexPaths with rowsanimation:(UITableViewRowAnimation)动画;您必须传递数组您必须在最后一个索引处添加对象,并在获得数组后以相反顺序添加该行。您的数据源是否反映了更改?我在
[self.commentsTableView endUpdates]再次收到错误;
它表示更新后现有节中包含的行数(6)必须等于更新之前该节中包含的行数(6),加上或减去从该节插入或删除的行数(1插入,0删除),加上或减去移入或移出该节的行数(0移入,0移出)“”这很奇怪,因为我检查了数组的计数,它增加了。你的UITableView包含组?你是指节?不只是1-(NSInteger)numberOfSectionsInTableView:(UITableView*)表视图{return 1;}
如果没有组/节,您可以尝试删除numberOfSectionsInTableView委托并再试一次吗?奇怪!您可能需要编辑问题并添加UITableViewDelegate方法,然后我们可以查看更多详细信息。