2011-06-17 27 views
2

我使用包含2個部分的UITableview創建控制器。在第二部分中,我有一列用戶列表,當我點擊特定行時,我想向列表中的用戶添加複選標記。我遇到了一個問題,點擊用戶並將單元格滾動出頁面後,單元格將返回「名稱」字段BLANK(請參見下面的截圖)。我知道這與細胞被重複使用的方式有關,但我無法解決問題。我的代碼發佈在下面。有任何建議嗎?目標C:在滾動出屏幕(和返回)後單元格文本消失

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString* ShareOnCellIdentifier = @"ShareOnholderCell"; 
    static NSString* WhoShouldAnswerCellIdentifier = @"WhoShouldAnswerCell"; 

    int row = [indexPath row]; 

    User *user = [self.friendsToShareWithArray objectAtIndex:row]; 

    if (indexPath.section == 0) 
    { 

     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ShareOnCellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShareOnCellIdentifier]; 
     } 
     cell.textLabel.text = [self.shareOnArray objectAtIndex:indexPath.row]; 

     return cell; 

    } 
    else 
    { 
     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:WhoShouldAnswerCellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:WhoShouldAnswerCellIdentifier]; 
     } 
     cell.textLabel.text = user.nickname; 
     if (user.isSelected) 
     { 
      cell.selectionStyle = UITableViewCellAccessoryCheckmark; 
     } 
     else 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
     return cell; 
    } 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    User *user = [self.friendsToShareWithArray objectAtIndex:indexPath.row]; 
    if (indexPath.section == 1) 
    { 
     UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 

     if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
     { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      user.isSelected = NO; 

     } 
     else 
     { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      user.isSelected = YES; 
     } 
    } 
} 

編輯:我如何創建我的用戶對象

User *user1 = [[User alloc]init]; 
    user1.nickname = @"Dionis"; 
    user1.uid = @"1"; 
    user1.isSelected = NO; 
+0

by cell.selectionStyle = UITableViewCellAccessoryCheckmark;'你的意思是'cell.accessoryType = UITableViewCellAccessoryCheckmark;'對嗎? – 2011-06-17 01:19:41

+0

是的,這是對的,愚蠢的錯誤從我身上。感謝您指出這一點 – Zhen 2011-06-17 01:38:32

+2

不是你的答案,但你泄漏細胞。每當你分配/初始化一個新單元時,你也需要自動釋放它。否則,您創建的每個表格單元格都會永遠存在 – 2011-06-17 01:46:12

回答

2

我看到的唯一問題是,你已經實現didSelectRowAtIndexPath:方法的方式實例。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 1) 
    { 
     User *user = [self.friendsToShareWithArray objectAtIndex:indexPath.row]; 
     user.isSelected = !user.isSelected; 

     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationNone]; 
    } 
} 
+0

@Deepak,謝謝你的迴應。我更改了didSelectRowAtIndexPath方法中的代碼。當我嘗試點擊單元格時,沒有任何反應。你有沒有看到其他的東西丟失? – Zhen 2011-06-17 01:38:09

+0

'User'中定義的'isSelected'屬性如何? – 2011-06-17 01:42:04

+0

我試圖添加代碼NSLog(@「user selected:%d」,user.isSelected);在didSelectRowAtIndexPath中,它始終返回值0.爲什麼它不會爲我更改布爾型「isSelected」的值?請參閱我編輯的qn關於如何設置我的用戶的第一個地方? – Zhen 2011-06-17 01:43:53

1

我有過這樣的問題所造成的沒有意識到,基本上每一個細胞映入眼簾的滾動UITableView時,它會被通過cellForRowAtIndexPath重新請求了過去。我看不出這裏正在犯了一個錯誤(你總是設置標籤的文本),但在這一點上,我會建議檢查標籤的文本後,將其設置.:

if (!cell.textLabel.text || [[cell.textLabel.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) 
    NSAssert(NO, @"Bad text!"); 

在調試運行如果文本爲空或零,這將在NSAssert「崩潰」。那麼你可能會檢查周圍的變量(可能是user是否等)?並且瞭解更多信息。

相關問題