2011-08-03 88 views
0

我在應用程序中創建了一個功能,當點擊單元格時加載自定義單元格。點擊單元格會顯示額外的按鈕,再次點擊會隱藏它,沒有問題。不過,我認爲當點擊當前選定行旁邊的行時,我有一個單元重用問題。看截圖,我點擊北加州然後按鈕顯示,當我點擊埃塞俄比亞的結果是第二個截圖。自定義表格視圖單元格的單元重用問題

http://i228.photobucket.com/albums/ee262/romano2717/reuse.jpg

這裏的代碼

if(selectedIndexPath != nil && [selectedIndexPath isEqual:indexPath]) { 
    selectedIndex = indexPath.row; 
    NSLog(@"selectedIndex %d",selectedIndex); 
    static NSString *CustomCellIdentifier = @"CustomCell"; 
    CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 
    if (customCell == nil) { 
     customCell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier] autorelease]; 
    } 
    customCell.locationLabelOutlet.text = [usgsFeeds objectAtIndex:[indexPath row]]; 
    customCell.dateLabelOutlet.text = [pubDateArr objectAtIndex:[indexPath row]]; 

    float thisMag; 
    thisMag = [(NSNumber *)[magnitudeArr objectAtIndex:[indexPath row]] floatValue]; 
    customCell.magnitudeImageOutlet.image = [self imageForMagnitude:thisMag]; 
    [customCell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    return customCell; 
} 

else{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     locationLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, 225, 20)] autorelease]; 
     locationLabel.tag = kLocationLabelTag; 
     locationLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0]; 
     locationLabel.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview:locationLabel]; 

     //same as above will follow 
    } 
    else{ 
     locationLabel = (UILabel *)[cell.contentView viewWithTag:kLocationLabelTag]; 
     dateLabel = (UILabel *)[cell.contentView viewWithTag:kDateLabelTag]; 
     magnitudeImage = (UIImageView *)[cell.contentView viewWithTag:kMagnitudeImgTag]; 
    } 
    locationLabel.text = [usgsFeeds objectAtIndex:[indexPath row]]; 
    dateLabel.text = [pubDateArr objectAtIndex:[indexPath row]]; 

    float thisMag; 
    thisMag = [(NSNumber *)[magnitudeArr objectAtIndex:[indexPath row]] floatValue]; 
    magnitudeImage.image = [self imageForMagnitude:thisMag]; 

    return cell; 
} 

,這是我的自定義單元格

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: 
    (NSString *)reuseIdentifier 
    { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
    locationLabelOutlet = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, 210, 17)]; 
    locationLabelOutlet.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0]; 
    locationLabelOutlet.backgroundColor = [UIColor clearColor]; 
    //and other outlets 
    [thisContainer addSubview:locationLabelOutlet]; 
    [thisContainer addSubview:magnitudeImageOutlet]; 
    [thisContainer addSubview:dateLabelOutlet]; 

    [buttonsView addSubview:locButton]; 
    [buttonsView addSubview:detButton]; 
    [buttonsView addSubview:shabutton]; 
    [buttonsView addSubview:favButton]; 

    [thisContainer addSubview:buttonsView]; 
    [self.contentView addSubview:thisContainer]; 

這對於沒有選擇

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: 
    (NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    selectedIndexPath = indexPath; 

    if(selectedIndex == indexPath.row) 
    { 
    selectedIndexPath = nil; 
    [tableView reloadRowsAtIndexPaths: 
    [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    selectedIndex = -1; 
    return; 
    } 

    if(selectedIndexPath != nil && [selectedIndexPath isEqual:indexPath]) { 
    [tableView reloadRowsAtIndexPaths: 
    [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    } 

回答

0

好吧,也許是因爲你沒有'請重新加載先前選擇的單元格selectedIndexPath

我猜你的代碼在你敲擊同一行兩次時工作。

我會直接發表評論,在tableView:didSelectRowAtIndexPath:方法的代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    //added the following if() part 
    if(selectedIndex != -1 && selectedIndex != indexPath.row && selectedIndexPath != nil) 
    { 
     [tableView reloadRowsAtIndexPaths: 
     [NSArray arrayWithObject:selectedIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 

    selectedIndexPath = indexPath; 

    if(selectedIndex == indexPath.row) 
    { 
     selectedIndexPath = nil; 
     [tableView reloadRowsAtIndexPaths: 
     [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     selectedIndex = -1; 
     return; 
    } 

    if(selectedIndexPath != nil && [selectedIndexPath isEqual:indexPath]) 
    { 
     [tableView reloadRowsAtIndexPaths: 
     [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

我希望它會工作。

+0

謝謝,但它沒有工作,代碼工作在單擊。無論如何,我做了你所說的並用這段代碼重新載入先前選定的單元格。 (selectedIndex> = 0) 如果(selectedIndex> = 0) { NSIndexPath * previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0]; selectedIndex = indexPath.row; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade]; } 它的工作現在但動畫不那麼尖銳。 :d – Diffy

相關問題