插入 [表视图重新加载数据] 的位置



我得到了一个全局可变数组。当我将对象从其他视图控制器添加到其中时,我希望tableView更新。 我知道我必须使用[tableView reloadData]来更新我的表视图。但是我不知道我应该把这段代码放在哪里让我的tableView保持最新?

我是这样试过的:

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    AppDelegate *delegate= (AppDelegate*)[[UIApplication sharedApplication] delegate];
    return [delegate.globalArray count];
    [tableView reloadData];        
}    
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AppDelegate *delegate= (AppDelegate*)[[UIApplication sharedApplication] delegate];
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = delegate.globalArray [indexPath.row];
    return cell;
    [tableView reloadData];    
}

每当数组的内容发生变化时,都应该调用reloadData。 不要在这些方法中调用它(返回后什么都不做 - 很惊讶你没有收到关于无法访问的编译错误)

SB. 答案是正确的。 reloadData应在数组内容更改时使用。

要保持表的更新,它严格取决于您的应用程序流。我会尝试解释。

例如,假设当您启动应用程序时,表视图(由特定控制器管理,例如 A)是可见的,您可以使用加号按钮添加其他元素。点击加号按钮,它将打开一个模态控制器(比如 B),让你添加元素。要从 B 到 A 进行数据重新加载的通信,可以使用通知、块等。

无论如何,如前所述,这取决于您的应用程序。也许,如果管理表的控制器每次都是新的,那么您不需要这种类型的方法......

最新更新