ios 如何更新tableView中的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5482106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:24:47  来源:igfitidea点击:

how to update data in tableView?

iosobjective-cuitableview

提问by serge2487

I initialize data in my table with an array in viewDidLoadand then add the data to the cell. This a standard way of doing it that I read in a book. This is what I have:

我用数组初始化表中viewDidLoad的数据,然后将数据添加到单元格中。这是我在书中读到的标准做法。这就是我所拥有的:

- (void)viewDidLoad {
    [super viewDidLoad];
    //Create array and add data ("tableViewValues" is initialized in .h file)
    tableViewValues = [[NSMutableArray alloc]init];
    [tableViewValues addObject:@"0,000.00"];
    [tableViewValues addObject:@"9,318.79"];
}

// Customize the appearance of table view cells.
- (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 *cellValue = [tableViewValues objectAtIndex:indexPath.row];

    cell.textLabel.text = cellValue;

    return cell;
}

So when the view loads, those two currency values are in my table. Now in another function, I populate a another array with different currency numbers depending on what the user enter in a textfield. How would I update my current table view and replace those values with the values in my other array? Can anyone help me? Thanks!

所以当视图加载时,这两个货币值在我的表中。现在在另一个函数中,根据用户在文本字段中输入的内容,我用不同的货币数字填充另一个数组。我将如何更新我当前的表视图并将这些值替换为我其他数组中的值?谁能帮我?谢谢!

回答by Jamie

You can call

你可以打电话

[self.tableView reloadData];

to reload all data, however, you will need to program a way to have the array that you want populate the table. Maybe you want your -cellForRowAtIndexPath to call a private method that conditionally picks the correct array.

但是,要重新加载所有数据,您需要编写一种方法来让您想要填充表的数组。也许您希望您的 -cellForRowAtIndexPath 调用有条件地选择正确数组的私有方法。

回答by Jyoti Kumari

You have to remove all values from your array then you have to call table reload data

您必须从数组中删除所有值,然后您必须调用表重新加载数据

// In the method where you will get new values
[tableViewValues removeAllObjects];  
[tableViewValues add:@"new values"];  
 //reload table view with new values
[self.tableView reloadData];

回答by Markus

I've had this problem many times and I always make the same mistake.

我已经多次遇到这个问题,而且我总是犯同样的错误。

[self._tableview reloadData]works!

[self._tableview reloadData]作品!

The question is the place where you populate the table.

问题是您填充表格的位置。

I did it in -(void)viewDidLoad and the table was updatedwith the same data. Apparently nothing happens.

我是在 -(void)viewDidLoad 中完成的,并且表更新相同的数据。显然什么也没有发生。

Update the content of your table view (plist, dictionary, array, whatever) in the same place where you call [self._tableview reloadData], just before.

在之前调用 [self._tableview reloadData] 的同一位置更新表视图的内容(plist、字典、数组等)。

THAT IS THE KEY!!!

这就是关键!!!

Do a test:

做一个测试:

#pragma mark - Actions

- (IBAction)refreshParams:(id)sender {

   self.dictionary = nil;

   [self._tableView reloadData];
}

You can see how disappear the tableview content when you push the refresh button. Obviously, this is an example. I use a dictionary to populate the table and a button to refresh the content.

当您按下刷新按钮时,您可以看到 tableview 内容是如何消失的。显然,这是一个例子。我使用字典来填充表格和一个按钮来刷新内容。

回答by Balaji Sankar

in that function, after assigning values to the new array, all you have to do is

在该函数中,在为新数组赋值后,您所要做的就是

 [tableViewValues removeAllObjects];
 tableViewValues = [[NSMutableArray alloc] arrayByAddingObjectsFromArray:newArray];
 [self.tableView reloadData];

回答by Eimantas

You'd need to (release and) recreate same tableViewValuesarray and then call reloadDatamethod on tableView like this:

您需要(释放并)重新创建相同的tableViewValues数组,然后reloadData像这样在 tableView 上调用方法:

[self.tableView reloadData];

This will work if you're on table view controller and self.tableViewpoints to the table in question.

如果您在表视图控制器上并self.tableView指向有问题的表,这将起作用。

回答by Aditya

[self.tableView reloadData];

will help you for this.

会帮助你。