2012-03-19 31 views
1

我必須在這裏錯過簡單的東西,但它逃脫了我。在用戶將新人員輸入到可變數組後,我想更新表格。可變數組是數據源。我相信我的問題在cellForRowAtIndexPath之內。Tableview在添加人後沒有正確更新

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

    TextFieldCell *customCell = (TextFieldCell *)[tableView dequeueReusableCellWithIdentifier:@"TextCellID"]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if (indexPath.row == 0) { 
     if (customCell == nil) { 
      NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"TextFieldCell" owner:nil options:nil]; 
      for (id currentObject in nibObjects) { 
       if ([currentObject isKindOfClass:[TextFieldCell class]]) 
        customCell = (TextFieldCell *)currentObject; 
      } 
     } 
     customCell.nameTextField.delegate = self; 
     cell = customCell; 
    } 

    else { 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 

      cell.textLabel.text = [[self.peopleArray objectAtIndex:indexPath.row-1] name]; 
      NSLog(@"PERSON AT ROW %d = %@", indexPath.row-1, [[self.peopleArray objectAtIndex:indexPath.row-1] name]); 
      NSLog(@"peopleArray's Size = %d", [self.peopleArray count]); 
     } 
    } 

    return cell; 
} 

當我第一次加載視圖一切都很好。這是打印:

PERSON AT ROW 0 = Melissa 
peopleArray's Size = 2 
PERSON AT ROW 1 = Dave 
peopleArray's Size = 2 

後,我將某人添加到該數組我得到這個:

PERSON AT ROW 1 = Dave 
peopleArray's Size = 3 
PERSON AT ROW 2 = Tom 
peopleArray's Size = 3 

當我添加第二個人,我得到:

PERSON AT ROW 2 = Tom 
peopleArray's Size = 4 
PERSON AT ROW 3 = Ralph 
peopleArray's Size = 4 

爲什麼沒有打印陣列中的每個人?這種模式繼續下去,它只打印兩個人,它總是最後兩個人。我錯過了什麼?

--- 修訂 ---

確定。我的細胞沒有正確更新,我想我問的建議會幫助我。我想這不是主要問題。

我的問題是我的行沒有打印正確的信息。當視圖第一次加載我得到:

Melissa 
Dave 

,但我加湯後,我得到:

Melissa   Dave 
Melissa or Dave 
Tom    Tom 

,我添加拉爾夫之後,我得到:

Melissa   ? 
Tom  or ? 
Tom    Tom 
Ralph   Ralph 

這是怎麼回事上?

+0

如何更新表? – TommyG 2012-03-19 23:17:41

+1

** Tableview沒有正確更新**。添加新項目時,您在應用中看到了什麼?你只談到了'NSLog'輸出,但沒有解釋實際UI上的問題。 – sch 2012-03-19 23:34:07

+0

我想這個問題是細胞正確更新。當我打印'self.peopleArray'後,我添加每個人後,它顯示每個人。但是當它顯示在單元格中時,它不會正確顯示所有名稱。 – tazboy 2012-03-19 23:52:19

回答

1

問題是您沒有更新重用單元的標籤。當單元格新創建時,您只設置一次。因此,將您的代碼更改爲:

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
} 
cell.textLabel.text = [[self.peopleArray objectAtIndex:indexPath.row-1] name]; 
NSLog(@"PERSON AT ROW %d = %@", indexPath.row-1, [[self.peopleArray objectAtIndex:indexPath.row-1] name]); 
NSLog(@"peopleArray's Size = %d", [self.peopleArray count]); 
+0

他明顯這樣做,因爲NSLogs裏面cellForRowAtIndexPath – Alladinian 2012-03-19 23:29:24

+0

無後顧之憂,兩雙眼睛總是比一個好:) – Alladinian 2012-03-19 23:34:42

+0

看起來我做了一個新的朋友!謝謝sch。括號是罪魁禍首。我不知道我是否會抓住那件事。非常感謝。哦,現在它每次打印每個人和他們的行,包括0行,而不是僅僅是「更新的」行。再次感謝! – tazboy 2012-03-20 00:09:53

1

您在cellForRowAtIndexPath中調用NSLog就是原因。 由於單元格是新的,它第一次記錄所有內容,但在此之後,單元格保持緩存狀態,直到它們離開屏幕。由於你的表很小,所以第一個單元總是在屏幕上,並且從不重新創建。如果你想監視你的數據源而不是單元格,試着把NSLog移到其他地方(你可以確切地看到我的意思,如果你加載一個大表,滾動它並觀察你的控制檯)

相關問題