2010-04-22 75 views
0

我有我在界面生成器中創建的自定義UITableViewCell。我成功地將單元排隊,但在滾動時,單元似乎開始調用不同的indexPath。在這個例子中,我將當前的indexPath.section和indexPath.row饋送到customCellLabel中。當我上下滾動表格時,一些單元格會改變。數字可以遍佈整個地方,但細胞並沒有在視覺上跳過。自定義UITableViewCell在滾動時更改indexPath?

如果我註釋掉if(cell == nil),那麼問題就會消失。

如果我使用標準單元格,問題就會消失。

想法爲什麼會發生這種情況?

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CalendarEventCell"]; 
    if (cell == nil) { 
     NSLog(@"Creating New Cell !!!!!"); 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    // Set up the cell... 
    [customCellLabel setText:[NSString stringWithFormat:@"%d - %d",indexPath.section, indexPath.row]]; 


    return cell; 
} 
+0

customCellLabel指的是什麼? – jamesmoschou 2010-04-22 06:30:41

+0

customCellLabel是我在界面生成器中創建的CalendarEventCell中包含的標籤。 – Chris 2010-04-22 14:59:07

回答

2

當您在如果(細胞==零)的條件,你分配一個reuseIdentifier對小區的「CalendarEventCell」細胞?我可以看到你的筆尖的名字是CalendarEventCell,但我猜你也需要設置

cell.reuseIdentifier = @"CalendarEventCell";

如果沒有,那麼我不知道,如果它可以雙端隊列正確的細胞。另外,不知道customCellLabel引用了什麼。

+0

當您使用筆尖創建可重用單元時,可以在Interface Builder中設置reuseIdentifier。我已將Reuse Identifier設置爲與文件名相同。 我相信它是正確的出隊單元,因爲當我加載表時,我得到了一些NSLogs,當我開始滾動上下滾動時沒有額外的NSLogs。 – Chris 2010-04-22 14:58:37

+0

另外,你不能以這種方式設置cell.resuseIdentifier。 reuseIdentifier是隻讀的。 – Chris 2010-04-22 15:43:50

+0

對不起,我錯過了那一個。你是對的。它是一個只讀屬性,只能在創建要重用的單元格時設置。 – 2010-04-23 03:33:50

相關問題