在UITableView中更新标签

时间:2013-02-15 13:03:26

标签: iphone xcode uitableview

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

//    NSLog(@"msgcnt123 %@\n",[messageCount objectAtIndex:indexPath.row]);

    NSArray *seperateArray = [[clist objectForKey:[[clist allKeys]objectAtIndex:indexPath.row]]componentsSeparatedByString:@"@"];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    NSLog(@"key %@\n",[[clist allKeys]objectAtIndex:indexPath.row]);

    cell.textLabel.text = [seperateArray objectAtIndex:0];
//    cell.textLabel.text = [contactlist objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

//    NSLog(@"sep %@\n",seperateArray);


    if (![[seperateArray objectAtIndex:1] isEqualToString:@"0"]) {
        NSLog(@"msgCount %@\n",[seperateArray objectAtIndex:1]);
        lblCnt = [[UILabel alloc]initWithFrame:CGRectMake(260, 13, 20, 20)];
        lblCnt.backgroundColor = [UIColor lightGrayColor];
        lblCnt.textColor = [UIColor blackColor];
        lblCnt.text = [seperateArray objectAtIndex:1];
        lblCnt.textAlignment = UITextAlignmentCenter;
        lblCnt.layer.masksToBounds = YES;
        lblCnt.layer.cornerRadius = 2.0f;
        [cell.contentView addSubview:lblCnt];
        lblCnt.tag = 1000;

    }
    else
    {
        /*NSLog(@"msgCount1 %@\n",[seperateArray objectAtIndex:1]);
        [lblCnt removeFromSuperview];
        lblCnt.hidden = YES;*/
        for (UIView *view in [cell.contentView subviews]) 
        {
            if (view.tag == 1000) 
            {
              [view removeFromSuperview];
            }
        }

    }

    return cell;
}

我在UITableView中显示带有标签的联系人姓名。标签显示每个联系人收到的消息数。我的视图名称是chatViewcontroller。如果我在该视图中标签计数未更新。例如:对于联系人姓名“John”标签计数为“2”。当John收到一条消息,标签计数应该更新为“3”。它不会发生。如果我从另一个视图移动它会更新。是否可以更新tableView chatviewController本身?

1 个答案:

答案 0 :(得分:1)

确保您更新了数据源(clist),然后致电[tableview reloadData]