簡體   English   中英

UITableView頂部的插入行崩潰

[英]UITableView insert row at top crashes

我試圖在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並確保它反映您的comments.count

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

同樣,所有UI更新都必須在主線程上完成,如果您在其他線程中調用update方法,則會發生更改,您會看到該錯誤。

您應該在動畫塊內更新數據源數組:

[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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM