2011-07-10 149 views
0

我建立一个聊天应用程序,我有一个小问题,建立的cellForRowAtIndexPath:自定义的UITableViewCell

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

回答

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;