自定义uitableviewcell

时间:2011-07-10 22:32:59

标签: iphone objective-c

我正在建立一个聊天应用程序,我有一点问题,建立cellforrowatindexpath:

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

static NSString *CellIdentifier = @"UserCell";


UserCell *cell = (UserCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"UserCell" 
                                                             owner:nil 
                                                           options:nil];
    for (id currentObject in topLevelObjects) {
        if ([currentObject isKindOfClass:[UITableViewCell class]]) {
            cell = (UserCell *) currentObject;
            break;
        }
    }
}

NSString *nameValue = [self.names objectAtIndex:indexPath.row];
NSString *statusValue = [self.usersStatus objectAtIndex:indexPath.row];
cell.name.text = nameValue;
cell.status.text = statusValue;


return cell;

}

并且在每个单元格中我都有一个隐藏的标签,并且在:

-(void)chatHandlerNewMsg:(ChatHandler*)chat withInt:(NSString*)userId{
NSInteger tmp = [self.usersID indexOfObject:userId];
NSIndexPath *index = [NSIndexPath indexPathForRow:tmp inSection:0];
UserCell *cell = (UserCell*)[self.table cellForRowAtIndexPath:index];
[cell.newMsg setHidden:NO];
[self.table reloadData];

}

我检查新信息,如果是,我将uilabel从隐藏显示到显示。

问题是表格视图在表格视图的其他单元格中显示了这个uilabel(默认情况下是隐藏的),而不仅仅是我们选择的特定单元格:

UserCell *cell = (UserCell*)[self.table cellForRowAtIndexPath:index];

1 个答案:

答案 0 :(得分:0)

当您将单元格出列时,您需要手动重置上一行可能设置的任何状态。

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell) {
    //Reset cell state (make label hidden)
}
else
{
    //Create new cell and configure (hide labels)
}

//Set any common attributes here (title)

return cell;