UITableViewCell ImageView无法更新

时间:2013-09-04 20:56:48

标签: ios objective-c json uitableview uiimage

我的app中有一个tableview,用于使用自定义tableviewcells进行消息传递。此表视图使用JSON中的数组填充。我在单元格中有一个UIImageView,如果邮件未读,则显示蓝点图像。

以下是一些代码:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    messagesArray = [self getMessages];
    [messagesTableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"MessagesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    NSDictionary *messagesDictionary = [messagesArray objectAtIndex:indexPath.row];

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];     
    nameLabel.text = [messagesDictionary objectForKey:@"fromUserName"];

    UIImageView *readImage = (UIImageView *)[cell viewWithTag:103];
    NSNumber *boolNumber = [messagesDictionary valueForKey:@"readFlag"];
    BOOL read = [boolNumber boolValue];

    if (!read)
        readImage.image = [UIImage imageNamed:@"Message Read Circle"];

    return cell;
}

当选择该消息时,我向服务器发送一条消息,让它知道该消息已被读取,但当我返回时,它仍然有未读图像。如果我在模拟器中退出应用程序并重新加载应用程序,未读取的图像将从我选择的消息中消失,因此我知道标记为已读消息。为什么[messagesTableView reloadData]不起作用?

2 个答案:

答案 0 :(得分:3)

由于表视图单元格重用,您应该在任何情况下设置图像, 而且不仅仅是read == NO。类似的东西:

if (read)
    readImage.image = [UIImage imageNamed:@"Message Read Circle"];
else
    readImage.image = [UIImage imageNamed:@"Message Unread Circle"];

答案 1 :(得分:1)

看起来你实际上在viewDidAppear中的tableview上调用reloadData就像你说的那样。

此外,就像Mike Z上面提到的那样,您可能遇到了getMessages调用时间问题。这种方法是同步还是异步?发布一些代码也可能有所帮助。

此外,如果您的消息已被阅读,您需要确保将readImage设置为nil。请记住,这些单元格已出列,因此如果不为read属性的true和false状态设置imageView,则可能会得到错误的结果。

相关问题