Проблема повторного использования ячейки для настраиваемой ячейки табличного представления - PullRequest
0 голосов
/ 03 августа 2011

Я создал функцию в своем приложении, которая загружает пользовательскую ячейку при касании ячейки. нажатие на ячейку покажет дополнительные кнопки, а повторное нажатие скроет ее, с этим проблем нет. Тем не менее, я думаю, что у меня есть проблема повторного использования ячеек при нажатии строк рядом с выбранной в данный момент строкой. посмотрите на скриншот, я нажимаю на Северную Калифорнию, затем появляются кнопки, когда я нажимаю на Эфиопию, получается второй скриншот.

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

1 Ответ

0 голосов
/ 03 августа 2011

Ну, может быть, это потому, что вы не перезагрузили ранее выбранную ячейку в 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];
    }
}

Надеюсь, это сработает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...