UILables无法在UITableViewCell中正确更新

Ern*_*nyi 0 iphone objective-c uitableview uilabel

几天来一直有这个问题,似乎无法找到解决方案.这可能是一些非常基本的东西,但仍然无法提出解决方案.

我有一堆标签嵌套在表格视图单元格中,带有编辑导航控制器按钮,可以转到另一个表格视图.此表视图具有将数据存储到SQLite数据库的文本字段.标签从数据库返回某些值.现在这部分完美无缺.但是,当我更新文本字段并导航回标签时,标签不会更新,如果我滚动表格以使修改后的单元格不在视图中,那么它会更新,所以认为问题是单元格仍然具有旧值缓存,只有在它出列时才释放它.

部分代码:(至少我认为这是重要的,因为这是创建单元格的地方)

欢迎任何帮助.谢谢

- (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.
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

打破 默认值:break;

ale*_*ntd 7

每当您对基础数据源进行更改时,都需要手动更新表视图.UITableView的reloadData方法是快速而低效的方法.正确的方法是:

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];
Run Code Online (Sandbox Code Playgroud)

您可以在控制器的viewWillAppear方法中执行此操作.