[英]how to update data in tableView?
I initialize data in my table with an array in viewDidLoad
and then add the data to the cell.我使用
viewDidLoad
的数组初始化表中的数据,然后将数据添加到单元格中。 This a standard way of doing it that I read in a book.这是我在书中读到的标准做法。 This is what I have:
这就是我所拥有的:
- (void)viewDidLoad {
[super viewDidLoad];
//Create array and add data ("tableViewValues" is initialized in .h file)
tableViewValues = [[NSMutableArray alloc]init];
[tableViewValues addObject:@"$280,000.00"];
[tableViewValues addObject:@"$279,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!
谢谢!
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 调用一个私有方法,该方法有条件地选择正确的数组。
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];
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 updated with the same data .我是在 -(void)viewDidLoad 中完成的,并且表更新了相同的数据。 Apparently nothing happens.
显然什么也没有发生。
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.当您按下刷新按钮时,您可以看到 tableview 内容是如何消失的。 Obviously, this is an example.
显然,这是一个例子。 I use a dictionary to populate the table and a button to refresh the content.
我使用字典来填充表格和一个按钮来刷新内容。
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];
You'd need to (release and) recreate same tableViewValues
array and then call reloadData
method on tableView like this:您需要(释放并)重新创建相同的
tableViewValues
数组,然后像这样在 tableView 上调用reloadData
方法:
[self.tableView reloadData];
This will work if you're on table view controller and self.tableView
points to the table in question.如果您在表视图控制器上并且
self.tableView
指向有问题的表,这将起作用。
[self.tableView reloadData];
会帮助你。
tableView.reloadData()
or self.tableView.reloadData()
, where tableView
is a property in your ViewController. tableView.reloadData()
或self.tableView.reloadData()
,其中tableView
是您的 ViewController 中的一个属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.