2011-07-10 147 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;