iphone - UITableView 中的数据加载就像 iBooks 搜索表中一样

标签 iphone objective-c uitableview

当我添加一些新数据时,如何告诉 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/

相关文章:

iphone - 将对象从NSMutableDictionary添加到NSMutableArray

iOS - 无约束的可滑动 UITableCells

iphone - 如何制作一个显示下拉菜单的按钮?

iphone - Xcode 4.5 无法识别我的设备 iphone 5 (6.1.3)

ios - 如何在 iOS 的文档目录中保存带有唯一标签的文件?

ios - 将数据加载到类中并使用它的策略

ios - IB_DESIGNABLE 绘制图像

ios - 如何使用swift在表格 View 中实现图像延迟加载

ios - 滚动时自定义表格未正确显示

iphone - 在 Web 服务之前加载 UITableViewController