2011-08-03 92 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

相关问题