views:

30

answers:

3

Hi there, been having this problem for a few days now and can't seem to find a solution for it. It's probably some very basic stuff but still can't come up with a solution.

I have a bunch of labels nested inside table view cells with an edit navigation controller button that goes to another table view. This table view has text fields that store data into an SQLite database. The labels return certain values from the database. Now this part works perfectly. But when I update a text field and navigate back to the labels the label is not updated,if I scroll the table so that the modified cell is out of the view then it gets updated, so think the problem is that the cell still has the old value cached and only releases it once its dequeued.

Part of the code:(at least i think this is what matters as this is where the cells are created)

Any help is welcome. Thanks

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"ContactsDetailCell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.

int row = [indexPath row]; int section = [indexPath section];

NSDictionary *resultSet = [sqliteManager queryRow:[NSString stringWithFormat:@"SELECT * FROM contacts WHERE id = %d;", contactsId]];

switch (section) { case 0: switch (row) { case 0: cell.textLabel.text = @"Name:";

UILabel *nameLabel = [[UILabel alloc] initWithFrame: CGRectMake(90, 10, 200, 25)];
  nameLabel.text = [resultSet objectForKey:@"name"];
  nameLabel.textColor = [UIColor blackColor];
  nameLabel.font = [UIFont systemFontOfSize:17.0];
  nameLabel.backgroundColor = [UIColor whiteColor];
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  [cell.contentView addSubview:nameLabel];
  [nameLabel release];
 break;
case 1:

 cell.textLabel.text = @"Address:";
 cell.selectionStyle = UITableViewCellSelectionStyleNone;

UILabel *addressLabel = [[UILabel alloc] initWithFrame: CGRectMake(90, 10, 200, 25)];

  addressLabel.text = [resultSet objectForKey:@"address"];
  addressLabel.textColor = [UIColor blackColor];
  addressLabel.font = [UIFont systemFontOfSize:17.0];
  addressLabel.backgroundColor = [UIColor whiteColor];
  [cell.contentView addSubview:addressLabel];
 [addressLabel release];
 break;

default:
 break;

} break; default: break;

A: 

You have to tell the view to update the data and bind to the UITableView again after a change has been made. You can do this in the viewWillAppear method which is called when you navigate backwards.

George
A: 

You can try adding this code in the implementation file of the principal table:

-(void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
[super viewWillAppear:animated];
}

In some cases this can not work and I resolved creating the cell programmatically with a class and without a nib ;)

Hope this can help you!

Matthew
A: 

Whenever you make changes to the underlying data source, you need to update the table view manually. UITableView's reloadData method is the quick and inefficient way to do it. The correct way is:

NSArray *cells = [myTableView visibleCells];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (UITableViewCell *cell in cells) {
    [indexPaths addObject:[myTableView indexPathForCell:cell]];
}
[myTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:NO];
[indexPaths release];

You would do this in your controller's viewWillAppear method.

alexantd