选择后,您可以在UITableViewCell上设置高度变化动画吗?

390 cocoa-touch uitableview ios

UITableView在我的iPhone应用程序中使用了一个,我有一个属于一个组的人员列表.我希望这样当用户点击特定的人(从而选择单元格)时,单元格的高度会增加,以显示多个UI控件来编辑该人的属性.

这可能吗?

Sim*_*Lee 876

我找到了一个非常简单的解决方案作为UITableView我正在努力的副作用.....

将单元格高度存储在通常通过其报告原始高度的变量中tableView: heightForRowAtIndexPath:,然后当您想要设置高度变化动画时,只需更改变量的值并调用此...

[tableView beginUpdates];
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

你会发现它没有完全重新加载,但足以UITableView知道它必须重绘单元格,抓住单元格的新高度值....并猜猜是什么?它为您改变了动态.甜.

我在我的博客上有更详细的解释和完整的代码示例... Animate UITableView Cell Height Change

  • 这是一个奇怪的解决方案,但它也是Apple在WWDC 2010"Mastering Table View"会话中推荐的.我将提交一份错误报告,将其添加到文档中,因为我花了大约2个小时进行研究. (44认同)
  • 这样可行,但是如果我制作一个更大的单元格,可以说从44到74然后再将它变小到44,则分隔线的行为非常奇怪.有人可以证实这一点吗? (7认同)
  • 这太棒了.但是有什么方法可以控制表格的动画速度吗? (6认同)
  • 现在它也写在官方文档中:`你也可以使用这个方法,然后使用endUpdates方法来动画行高度的变化,而无需重新加载单元格.https://developer.apple.com/library/ios/文档/ UIKit的/参考/ UITableView_Class/index.html中 (6认同)
  • 我已经尝试过这个解决方案,它只在某些情况下有效,当你按下一个单元格时,有50%的可能性.有人有同样的错误吗?它是因为iOS7? (5认同)
  • 这是优于[self.tableView reloadRowsAtIndexPaths:[NSArray的arrayWithObject:indexPath] withRowAnimation:YES]; (3认同)
  • 如果在键入时在单元格中使用UITextView,则需要在tableView endUpdates之后将textView滚动到顶部:[textView setContentOffset:CGPointMake(0,0)animated:YES]; (2认同)
  • 我注意到包装更新调用UIView基于块的动画会产生更平滑的结果.就像`[UIView animateWithDuration:0.3 animations:^ {[self.tableView beginUpdates];/*改变一些逻辑*/[self.tableView endUpdates]; }];` (2认同)
  • 我只想补充一点,你应该*不*调用`reloadRowsAtIndexPaths:`如果你使用静态表格单元,因为这里提到的原因(我认为这是一个bug):http://stackoverflow.com/a/18713132/238948 (2认同)

Mar*_* A. 61

我喜欢Simon Lee的回答.我实际上并没有尝试那种方法,但看起来它会改变列表中所有单元格的大小.我希望改变被挖掘的细胞.我有点喜欢西蒙,但差别很小.这将在选择单元格时更改其外观.它确实有动画.只是另一种方式来做到这一点.

创建一个int来保存当前所选单元格索引的值:

int currentSelection;
Run Code Online (Sandbox Code Playgroud)

然后:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int row = [indexPath row];
    selectedNumber = row;
    [tableView beginUpdates];
    [tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)

然后:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([indexPath row] == currentSelection) {
        return  80;
    }
    else return 40;


}
Run Code Online (Sandbox Code Playgroud)

我相信你可以在tableView中进行类似的更改:cellForRowAtIndexPath:改变单元格的类型,甚至加载单元格的xib文件.

像这样,currentSelection将从0开始.如果您不希望默认情况下选择列表的第一个单元格(在索引0处),则需要进行调整.

  • 当前选择已存储在tableView.indexPathForSelectedRow中. (12认同)
  • "我实际上并没有尝试那种方法,但看起来它会改变列表中所有单元格的大小" - 那么你看起来并不是很难. (8认同)
  • 检查我的帖子附带的代码,我做了这个,选中时单元格的高度加倍.:) (2认同)

小智 22

添加属性以跟踪所选单元格

@property (nonatomic) int currentSelection;
Run Code Online (Sandbox Code Playgroud)

将其设置为(例如)中的标记值viewDidLoad,以确保UITableView在"正常"位置开始

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //sentinel
    self.currentSelection = -1;
}
Run Code Online (Sandbox Code Playgroud)

heightForRowAtIndexPath您可以设置所选单元格所需的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowHeight;
    if ([indexPath row] == self.currentSelection) {
        rowHeight = self.newCellHeight;
    } else rowHeight = 57.0f;
    return rowHeight;
}
Run Code Online (Sandbox Code Playgroud)

didSelectRowAtIndexPath您保存当前选择并保存动态高度(如果需要)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // do things with your cell here

        // set selection
        self.currentSelection = indexPath.row;
        // save height for full text label
        self.newCellHeight = cell.titleLbl.frame.size.height + cell.descriptionLbl.frame.size.height + 10;

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];
    }
}
Run Code Online (Sandbox Code Playgroud)

didDeselectRowAtIndexPath将选择索引设置回sentinel值并将单元格设置为正常形式

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {       
        // do things with your cell here

        // sentinel
        self.currentSelection = -1;

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];
    }
}
Run Code Online (Sandbox Code Playgroud)


raf*_*raf 15

而不是beginUpdates()/ endUpdates(),现在推荐的调用是:

tableView.performBatchUpdates(nil, completion: nil)
Run Code Online (Sandbox Code Playgroud)

Apple 说,关于 beginUpdates/endUpdates:“尽可能使用 performBatchUpdates(_:completion:) 方法而不是这个方法。”

请参阅:https : //developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates


law*_*nce 14

reloadData不好,因为没有动画......

这就是我目前正在尝试的:

NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

它几乎正常.几乎.我正在增加单元格的高度,有时在表格视图中有一点"打嗝",因为单元格被替换,好像保存了表格视图中的某些滚动位置,新单元格(这是第一个单元格)在表中)最终其偏移量太高,并且滚动视图反弹以重新定位它.


axi*_*ixc 11

我不知道连续调用beginUpdates/endUpdates的所有内容是什么,你可以使用-[UITableView reloadRowsAtIndexPaths:withAnimation:].这是一个示例项目.


fer*_*min 10

我解决了reloadRowsAtIndexPaths.

我保存在didSelectRowAtIndexPath所选单元格的indexPath中并reloadRowsAtIndexPaths在最后调用(您可以发送NSMutableArray以获取要重新加载的元素列表).

heightForRowAtIndexPath您可以检查是否indexPath在列表与否的expandIndexPath细胞的和发送的高度.

您可以查看以下基本示例:https: //github.com/ferminhg/iOS-Examples/tree/master/iOS-UITableView-Cell-Height-Change/celdascambiadetam 这是一个简单的解决方案.

如果有帮助,我会添加一些代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath
{
    if ([indexPath isEqual:_expandIndexPath])
        return 80;

    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Celda";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [cell.textLabel setText:@"wopwop"];

    return cell;
}

#pragma mark -
#pragma mark Tableview Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *modifiedRows = [NSMutableArray array];
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
    _expandIndexPath = indexPath;
    [modifiedRows addObject:indexPath];

    // This will animate updating the row sizes
    [tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
Run Code Online (Sandbox Code Playgroud)


mid*_*n p 5

Swift 4 及以上

将以下代码添加到 tableview 的 didselect 行委托方法中

tableView.beginUpdates()
tableView.setNeedsLayout()
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)


los*_*sit -2

获取所选行的索引路径。重新加载表。在 UITableViewDelegate 的 heightForRowAtIndexPath 方法中,将所选行的高度设置为不同的高度,而对于其他行则返回正常的行高

  • -1,不起作用。调用“[table reloadData]”会导致高度立即发生变化,而不是动画。 (2认同)