【问题标题】:UITableviewCell height not getting reset on scrollUITableviewCell 高度未在滚动时重置
【发布时间】:2015-12-29 21:11:53
【问题描述】:

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

- (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 请帮帮我

【问题讨论】:

  • 如果问题出在标签上,请发布在选择(取消选择)时显示(隐藏)标签的代码。
  • 添加了 didSelectRowAtIndexPath 方法

标签: ios objective-c uitableview


【解决方案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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2011-06-04
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多