![](/img/trans.png)
[英]Strange error: insertRowsAtIndexPaths in UITableView crashes with NSInternalInconsistencyException
[英]NSInternalInconsistencyException in insertRowsAtIndexPaths
我正在嘗試為我的表視圖實現insertRowsAtIndexPaths
。
在此實現之前,我曾經在用戶點擊表末尾后調用[self.tableView reloadData]
。 這種方法有點影響了我的應用程序的質量,因為它需要進行大量的行和大量的滾動。(每次重新凍結之前都會重新加載數據)
所以我改用下面的代碼:
p.lastid = appRecord.ids;
[p main];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self.tableView beginUpdates];
NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
[self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
});
});
}
return cell;
}
舊的工作代碼是:
NSArray *temp =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp;
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
p.appRecordList
是一個NSArray
,新行項目來自hayoola。
對於numberOfRowInSection
我使用以下代碼:
#define kCustomRowCount 10
@property (nonatomic, assign) int intindex;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = [self.entries count];
if (count == 0)
{
return kCustomRowCount;
}
return count;
}
我的代碼中也沒有cellforRowAtIndexPath
。 我應該提一下,我的程序每次都會從服務r獲取10行。
這是控制台錯誤代碼:
由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“試圖將第10行插入第0節,但更新后第0節只有10行” *
編輯:(因答案而異)
在將更新方法更改為此之后:
[self.tableView beginUpdates];
NSArray *temp_1 =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp_1;
NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
[self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
並為此insertRowsAtIndexPaths
:
NSUInteger count = [self.entries count];
return count;
出現此錯誤:
由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“無效更新:第0節中的行數無效。更新(20)之后現有節中包含的行數必須等於該節中包含的行數更新前的部分(10),加上或減去從該部分插入或刪除的行數(已插入1,已刪除0)以及正負移動到該部分的行數(移入0,已移動0)出)。“*
您需要確保在調用endUpdates
之前已將其他記錄添加到self.entries
。 例外是告訴您您嘗試添加第11行,但numberOfRowsInSection
返回10
您需要確保插入的行數與添加到數組中的元素的行數相同
您的更新方法應為
[self.tableView beginUpdates];
NSMutableArray *newIndices=[NSMutableArray new];
for (id record in p.appRecordList) {
[self.entries addObject:record];
[newIndicies addObject:[NSIndexPath indexPathForRow:self.entries.count]];
}
[self.tableView insertRowsAtIndexPaths:newIndices withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.