开发者

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. 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 i开发者_高级运维nitialized 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.


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!

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. 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.

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.


You'd need to (release and) recreate same tableViewValues array and then call reloadData method on tableView like this:

[self.tableView reloadData];

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


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];


[self.tableView reloadData];

will help you for this.


In Swift,

tableView.reloadData() or self.tableView.reloadData(), where tableView is a property in your ViewController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜