2011-09-04 78 views
2

用自定義單元格加載tableview後,所有看起來很好,向下滾動都很好。 當我向上滾動上面的單元格顯示空白。它們實際上功能完備,因爲它們仍然可以被選中,然後進入細節頁面。 代碼cellForRowAtIndexPath爲返回的行執行,並返回單元格,正如我所期望的正確的細節。它只是不顯示。 任何想法/幫助,將不勝感激。當再次滾動tableview時,單元格顯示爲空白?

下面的代碼是我一直在使用的。

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


    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 
    if (cell == nil) { 

     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     for (id oneObject in nib) { 
      if ([oneObject isKindOfClass:[CustomCell class]]) { 
       cell = (CustomCell *)oneObject; 
      } 
     } 
    } 

    // Configure the cell. 


    Book *aBook = [appDelegate.sortedArray objectAtIndex:indexPath.row]; 


    //name 
    NSString *trimmedString = [aBook.Name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    cell.nameLabel.text = trimmedString; 
    //catagory 
    NSString *trimmedExperience = [aBook.PrimaryExperience stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    if ([trimmedExperience isEqualToString:@"1"]) { 
     cell.catagoryLabel.text = @"None"; 
    } 
    else if([trimmedExperience isEqualToString:@"2"]){ 
     cell.catagoryLabel.text = @"Limited"; 
    } 
    else if ([trimmedExperience isEqualToString:@"4"]) { 
     cell.catagoryLabel.text = @"Full"; 

    } 
    else { 
     cell.catagoryLabel.text = @""; 

    } 




    cell.distanceLabel.text = [NSString stringWithFormat:@"%1.1f",aBook.distance]; 


    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 


    return cell; 
} 
+0

您究竟如何渲染細胞?您是使用Interface Builder還是從頭開始? – medopal

+0

單元格從界面生成器中呈現,文本在cellForRowAtIndexPath中設置 - 奇怪的是,它們工作得很漂亮,直到他們滾出頂部。 – Davida

+1

你可以顯示cellForRowAtIndexPath方法嗎? – jrturton

回答

1

問題在於xib文件及其如何加載。爲了解決問題並獲得完全控制,以下代碼替代了定製單元的IB版本。

static NSString *CellTableIdentifier = @"CellTableIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease]; 
    CGRect nameLabelRect = CGRectMake(15, 5, 200, 15); 
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect]; 
    nameLabel.textAlignment = UITextAlignmentLeft; 
    nameLabel.font = [UIFont boldSystemFontOfSize:14]; 
    nameLabel.tag = kNameTag; 
    [cell.contentView addSubview:nameLabel]; 
    [nameLabel release]; 

    CGRect catagoryLabelRect = CGRectMake(15, 26, 100, 15); 
    UILabel *catagoryLabel = [[UILabel alloc]initWithFrame:catagoryLabelRect]; 
    catagoryLabel.textAlignment = UITextAlignmentLeft; 
    catagoryLabel.font = [UIFont systemFontOfSize:12]; 
    catagoryLabel.tag = kExperienceTag; 
    [cell.contentView addSubview:catagoryLabel]; 
    [catagoryLabel release]; 

    CGRect distanceLabelRect = CGRectMake(210, 26, 70, 15); 
    UILabel *distanceLabel = [[UILabel alloc] initWithFrame:distanceLabelRect]; 
    distanceLabel.textAlignment = UITextAlignmentRight; 
    distanceLabel.font = [UIFont boldSystemFontOfSize:12]; 
    distanceLabel.tag = kDistanceTag; 
    [cell.contentView addSubview:distanceLabel]; 
    [distanceLabel release]; 
} 

感謝您幫助我們思考這一點。現在滾動效果很好。

+0

很高興你得到它的工作......奇蹟爲什麼xib不工作,但? – jrturton