Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 789
serge2487 Avatar asked Mar 30 '11 05:03

serge2487


2 Answers

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.

like image 66
Jamie Avatar answered Sep 23 '22 01:09

Jamie


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];
like image 28
Jyoti Kumari Avatar answered Sep 27 '22 01:09

Jyoti Kumari