私たちのUITableViewは、特定のセクションの行数を返します。私たちが経験していることは、cellForRowAtIndexPathが呼び出されるころまでに番号が変更され、配列インデックスが範囲外になることになります。numberOfRowsInSectionとcellForRowAtIndexPathを同期させるにはどうすればよいですか?
これらの2つの方法を同期する良い方法はありますか。そのため、基礎となるデータは変更されません。 @synchronizedを使用することを検討しましたが、ロックを解除するタイミングはわかりません。
私たちがテーブルをリフレッシュするもう一つのことは、別のスレッドからです。
[self addUsers:usersToShow];
[[self invokeOnMainThreadAndWaitUntilDone:NO] refresh:self]; // is this the issue?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.users.count; // returns 10 for example
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *newCell= nil;
static NSString *cellId = @"cellId";
cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
cell = newCell;
self.newCell = nil;
}
User* user = [self.users objectAtIndex:indexPath.row]; // index out of bounds now because number of users has changed
}
これは私たちが目指していたアプローチと同じです。しかし、ユーザー数は0になる可能性があります。インターフェイスに違反するため、ここでは無限のセルを返すことはできません。助言がありますか? – tjg184
少し質問を更新しました。 [[self invokeOnMainThreadAndWaitUntilDone:NO] refresh:self]を使用しています。テーブルを更新します。 – tjg184
refresh:セルフ・コール[self.tableView reloadData] – tjg184