ios - UITableviewCell 高度未在滚动时重置

标签 ios objective-c uitableview

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

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

最佳答案

单元格只能在 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 属性并仅使用 TableView 方法,例如。 ..

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

关于ios - UITableviewCell 高度未在滚动时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34519162/

相关文章:

ios - 无法构建 kudan iOS unity3d 项目

ios - 在 MKMapView 上调整 MKCircle 的大小闪烁

objective-c - 可互操作的 Obj-C typedef NS_ENUM 到 swift

ios - 为什么 UITableViewCell 不可访问(对于 VoiceOver)

ios - 如何更新 UI,等待一段时间并在 iOS 中使用线程再次更新它?

ios - UIBarButtons 太靠近 UIPopoverController 内的 UINavigationBar 的边缘

ios - UITableView 在重新加载后重置 UITableViewCell

ios - 为什么只有一个代表?

ios - 类似 Instagram 的导航栏 (iOS 7)

ios - “NSInternalInconsistencyException” - 无法在 uinavigationcontroller ios 6 上推送 uitableviewcontroller