Iphone 自定义表视图单元的单元重用问题

Iphone 自定义表视图单元的单元重用问题,iphone,ios,xcode,Iphone,Ios,Xcode,我在我的应用程序中创建了一个功能,在点击单元格时加载自定义单元格。点击单元格将显示额外的按钮,再次点击将隐藏它,这没有问题。但是,当点击当前选定行旁边的行时,我认为这里存在单元格重用问题。看屏幕截图,我点击北加利福尼亚,然后按钮出现,当我点击埃塞俄比亚,结果是第二个屏幕截图 这是密码 if(selectedIndexPath != nil && [selectedIndexPath isEqual:indexPath]) { selectedIndex = in

我在我的应用程序中创建了一个功能,在点击单元格时加载自定义单元格。点击单元格将显示额外的按钮,再次点击将隐藏它,这没有问题。但是,当点击当前选定行旁边的行时,我认为这里存在单元格重用问题。看屏幕截图,我点击北加利福尼亚,然后按钮出现,当我点击埃塞俄比亚,结果是第二个屏幕截图

这是密码

    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];
这是为did选择的

    - (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];
    }
    }

嗯,可能是因为您没有在
selectedIndexPath
重新加载以前选择的单元格

我想当你在同一行上点击两次时,你的代码就可以工作了

我将在
tableView:didselectrowatinexpath:
方法中直接对代码进行注释:

- (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];
    }
}

我希望它能起作用。

谢谢,但它不起作用,代码只能在一次点击时运行。不管怎样,我照你说的做了,并用这个代码重新加载了以前选择的单元格。如果(selectedIndex>=0){NSIndexPath*previousPath=[NSIndexPath indexPathForRow:selectedIndex Instition:0];selectedIndex=indexPath.row;[tableView ReloadRowstAndExpaths:[NSArray arrayWithObject:previousPath]with RowAnimation:UITableViewRowAnimationFade];}它现在可以工作了,但是动画没有那么清晰D