2014-11-24 58 views
0

它基於UITableViewController。 我的表由兩個部分組成,UITableViewCell顯示錯誤的內容

(1)從陣列的第一頁部分的顯示對象,標籤文本是黑色,並且具有細節能夠文本

(2)第二部分具有單排,其使用進入新的ViewController添加更多的對象

但是,當我嘗試向數組中添加更多對象並重新載入數據時,初始視圖是正確的,因此表顯示錯誤單元格的錯誤內容。

那麼誰能告訴我我做錯了什麼或者什麼?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) return self.addItems.count; 
    else return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Add Items List"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier 
                  forIndexPath:indexPath]; 

    if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:cellIdentifier]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    if (indexPath.section == 0) { 
    Item *item = [self.addItems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%08i", item.identifier.intValue]; 
    cell.detailTextLabel.text = item.detailDescription; 
    } 
    else { 
    cell.textLabel.text = @"Create Item"; 
    cell.textLabel.textColor = [tableView tintColor]; 
    cell.detailTextLabel.text = nil; 
    } 

    return cell; 
} 

- (void)unwindToAddItemsViewController:(UIStoryboardSegue *)segue { 
    CreateItemViewController *source = [segue sourceViewController]; 
    [self.addItems addObjectsFromArray:source.createItems]; 
    [self.tableView reloadData]; 
} 
+0

這個方法'unwindToAddItemsViewController'的用法是什麼? – 2014-11-24 08:06:15

+0

如果您打算爲兩個部分使用相同的單元格,單元格中的重置方法會將單元格中的所有視圖重置爲其原始狀態,這樣將重用單元格。 – GoodSp33d 2014-11-24 08:08:53

回答

0

我糾正如果我猜「錯誤的內容」是指您的第一節中的單元格textLabel有錯誤的顏色?

這是因爲您沒有在您的section == 0代碼路徑中設置textLabel的textColor。單元格被重用,所以如果您使用相同的重用標識符,則必須在所有代碼路徑中設置屬性。設置後,第1部分的單元格保留標籤的textColor。因此,如果該單元格被重新用作第0節單元格,則必須將其更改回該節中所需的值。

而且由於您使用dequeueReusableCellWithIdentifier:forIndexPath:來使單元出列,您可以刪除if (!cell)部分。該方法永遠不會返回零。

您的代碼應該是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Add Items List"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier 
                  forIndexPath:indexPath]; 

    if (indexPath.section == 0) { 
    Item *item = [self.addItems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%08i", item.identifier.intValue]; 

    cell.textLabel.textColor = [UIColor blackColor]; // NEW 

    cell.detailTextLabel.text = item.detailDescription; 
    } 
    else { 
    cell.textLabel.text = @"Create Item"; 
    cell.textLabel.textColor = [tableView tintColor]; 
    cell.detailTextLabel.text = nil; 
    } 
    return cell; 
} 
+0

謝謝,這解決了我的問題。所以這就是事情的運作方式。現在我更瞭解這個出列可重用的東西!但它仍然存在問題。 通過顯示「錯誤的內容」,我的意思是它有錯誤的顏色,並且沒有任何字幕顯示,直到我點擊它。現在顏色是正確的,但字幕仍然沒有顯示,直到我點擊它。 – 2014-11-24 08:37:42

0

謝謝,解決我的問題。所以這就是事情的運作方式。現在我更瞭解這個出列可重用的東西!

但它仍然有問題。通過顯示「錯誤的內容」,我的意思是它有錯誤的顏色,並且在點擊它之前沒有任何字幕顯示。在標題下有一個字幕空間,但它是空白的,直到我點擊它。

現在顏色是正確的,但字幕仍然沒有顯示,直到我點擊它。