2011-05-15 32 views
0

我有9節和56行的表格。重用小區... :(

我要添加一個文本標籤爲每個小區。我創建了一個NSArray 和含有該號碼的數組。在每一節(sectionsArray)行

這裏是我的代碼,但它不能正常工作在所有?

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

    //Use existing cell (reusable) 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; 

    //If no existing cell, create a new one 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease]; 

     //Define cell accessory type 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     //Create a subView for the cell 
     CGRect subViewFrame = cell.contentView.frame; 
     subViewFrame.origin.x += kInset; 
     subViewFrame.size.width = kInset + kSelectLabelWidth; 

     UILabel *selectedLabel = [[UILabel alloc] initWithFrame:subViewFrame]; 

     //SubView design 
     selectedLabel.textColor = [UIColor blackColor]; 
     selectedLabel.highlightedTextColor = [UIColor whiteColor]; 
     selectedLabel.backgroundColor = [UIColor clearColor]; 

     [cell.contentView addSubview:selectedLabel]; 

     int indRow = 0; 
     for (int i =0; i < indexPath.section; i++) { 
      indRow += [[sectionsArray objectAtIndex:i] intValue]; 
     } 
     indRow += indexPath.row; 

     NSDictionary *cellText = [menuList objectAtIndex:indRow]; 

     selectedLabel.text = [cellText objectForKey:@"selection"]; 
     [selectedLabel release]; 

    } 
    return cell;   
} 

什麼問題在此代碼

iOSSimulator中,我看到單元格的文本有時在滾動時發生更改,標籤的順序也不正確。

回答

6

所有這一切在你的細胞充滿代碼是在:

if (cell == nil) { 

語句,所以你最終建立一個少數細胞,並在填補他們(當細胞==零),然後就返回出隊的人。

這是它應該是什麼:

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

    //Use existing cell (reusable) 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; 

    //If no existing cell, create a new one 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease]; 

     //Define cell accessory type 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     //Create a subView for the cell 
     CGRect subViewFrame = cell.contentView.frame; 
     subViewFrame.origin.x += kInset; 
     subViewFrame.size.width = kInset + kSelectLabelWidth; 

     UILabel *selectedLabel = [[UILabel alloc] initWithFrame:subViewFrame]; 

     //SubView design 
     selectedLabel.textColor = [UIColor blackColor]; 
     selectedLabel.highlightedTextColor = [UIColor whiteColor]; 
     selectedLabel.backgroundColor = [UIColor clearColor]; 

     [cell.contentView addSubview:selectedLabel]; 
    } 
    // At this point cell whether it is a reused cell or a new one 
    // cell points to the object we need to fill in. 

    int indRow = 0; 
    for (int i =0; i < indexPath.section; i++) { 
     indRow += [[sectionsArray objectAtIndex:i] intValue]; 
    } 
    indRow += indexPath.row; 

    NSDictionary *cellText = [menuList objectAtIndex:indRow]; 

    selectedLabel.text = [cellText objectForKey:@"selection"]; 
    [selectedLabel release]; 

    return cell;  
} 

這是什麼代碼是這樣做的:

 
Try to get a reusable cell 

If no reusable cell is available 
{ 
    create a new cell 
} 

Fill in the values for the cell 

所以當屏幕的滾動細胞被放入隊列重用。當一個細胞即將進入屏幕時,您的cellForRowAtIndexPath被調用。分配新單元比重用現有單元慢,因此您首先嚐試從重用隊列中獲取單元,但如果其中一個不可用,則創建一個新單元。然後你填寫你的價值觀。

+0

我是iOS開發新手,我真的沒有得到我必須做的。我必須改變一些事情嗎? 「if(cell == nil)」??謝謝。 – MosHa 2011-05-15 09:34:45

+0

我已經用更多的信息更新了答案。希望現在對你有意義。 – idz 2011-05-15 09:53:39

+0

不!當我滾動表格時,應用程序正在終止... – MosHa 2011-05-15 10:02:14