当我添加一些新数据时,如何告诉 UITableView 重新渲染自身? 我正在 ib loadView 方法中执行此代码。
[NSThread detachNewThreadSelector:@selector(addSomeData) toTarget:self withObject:nil];
这是 addSomeData 方法:
-(void)addSomeData{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 1; i <= 100; i++) {
[myArray addObject:[NSString stringWithFormat:@"Page %i",i]];
NSDate *d2 = [NSDate dateWithTimeIntervalSinceNow:0.2f];
[NSThread sleepUntilDate:d2];
}
[pool release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *string = [myArray objectAtIndex:indexPath.row];
cell.textLabel.text = string;
// Configure the cell...
return cell;
}
myArray ist 是包含字符串的 NSArray。 所以主要问题是 - 当我向 myArray 添加新的 NSString 时如何动态更新 tableview?
最佳答案
[self.tableView reloadData]
方法是一种完全重新加载表格的解决方案,而 [self.tableView insertRowAtIndexPath:withAnimation]
是另一种仅插入新行并激发变化。
关于iphone - UITableView 中的数据加载就像 iBooks 搜索表中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3726320/