UITableviewCell高度未在滚动时重置

时间:2015-12-29 21:11:53

标签: ios objective-c uitableview

我有一个tableview,可以在选择单元格时进行扩展,然后在再次选择时折叠。选择时,单元格应展开并显示标签,再次选择时,它会折叠并隐藏标签。扩展和折叠工作正常,但如果我在扩展单元格后滚动tableview它表现得很奇怪。一旦它离开视图并返回,单元格将具有扩展单元格的高度,但是应该在扩展单元格中显示的标签被隐藏。如果我再次选择单元格,它将折叠并显示标签。我用,

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:t cellForRowAtIndexPath:indexPath];
    if([self cellIsSelected:indexPath])
        return cell.frame.size.height+35;
    return cell.frame.size.height;
}

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
    // Return whether the cell at the specified index path is selected or not
    NSNumber *selectedIndex = [self.selectedIndexes objectForKey:indexPath];
    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Deselect cell
    NSLog(@"Select cell:%@",indexPath);
    [self.tableView deselectRowAtIndexPath:indexPath animated:TRUE];

    if([self pickTaskForIndexPath:indexPath].productSpecialMessage){
        BOOL isSelected = ![self cellIsSelected:indexPath];
        NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
        [self.selectedIndexes setObject:selectedIndex forKey:indexPath];
        PickTaskTableviewCell *cell= [self.tableView cellForRowAtIndexPath:indexPath];
        cell.message.hidden=false;
        cell.messageLabel.text=[self pickTaskForIndexPath:indexPath].productSpecialMessage;
        cell.messageLabel.lineBreakMode=NSLineBreakByTruncatingTail;
        cell.messageLabel.numberOfLines=3;
        if(cell.messageLabel.hidden==true){

            cell.messageLabel.hidden = false;
        } else {
            cell.messageLabel.hidden = true;
        }
        NSLog(@"message:%@",cell.messageLabel.text);
        [cell layoutIfNeeded];
    }

    self.tableView.rowHeight=UITableViewAutomaticDimension;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

}

indexPath 已添加到 didSelectRowAtIndexPath 上的selectedIndexes 请帮帮我

1 个答案:

答案 0 :(得分:1)

应仅在cellForRowAtIndexPath内配置单元格。当发生状态更改使得单元格需要看起来不同时,只需重新加载该单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PickTaskTableviewCell *cell = (PickTaskTableviewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];

    // everything else you do to configure the cell goes here, then ...

    // check the logic here, we want one condition that tells us whether to show the labels
    if([[self cellIsSelected:indexPath] && self pickTaskForIndexPath:indexPath].productSpecialMessage){
        // don't need these here
        //NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
        // [self.selectedIndexes setObject:selectedIndex forKey:indexPath];
        // PickTaskTableviewCell *cell= [self.tableView cellForRowAtIndexPath:indexPath];

        cell.message.hidden=false;
        cell.messageLabel.text=[self pickTaskForIndexPath:indexPath].productSpecialMessage;
        cell.messageLabel.lineBreakMode=NSLineBreakByTruncatingTail;
        cell.messageLabel.numberOfLines=3;
        cell.messageLabel.hidden=NO;
    } else {
        cell.message.hidden=YES;
        cell.messageLabel.hidden=YES;
    }
    NSLog(@"message:%@",cell.messageLabel.text);
    // don't need this here
    // [cell layoutIfNeeded];
    return cell;
}

选择(并且可能是取消选择)导致需要更新单元格,所以......

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // don't deselect it here, just reload it

    // more on this later...
    [self.selectedIndexes setObject:selectedIndex forKey:indexPath];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

// probably do the same in didDeselectRowAtIndexPath:

最后一个(可选)点。您无需维护自己的选定索引路径列表,UITableView可以为您执行此操作,因此您可以删除selectedIndexes属性并使用表格视图方法,例如....

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
    // Return whether the cell at the specified index path is selected or not
    return [[self.tableView indexPathsForSelectedRows] containsObject:indexPath];
}