ios - UITableViewCell 上的动画

标签 ios objective-c uitableview animation

我正在尝试将动画放在 UITableViewCell 中。动画是onClick table view cell 把tableCell 的frame 变成tableview frame。

我有以下代码:

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

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

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell*cell = [tableView cellForRowAtIndexPath:indexPath];

    if(cell.frame.size.height == tableView.bounds.size.height){
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }else{
        cell.frame = tableView.bounds;
        [[cell superview] bringSubviewToFront:cell];
    }

    [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
         [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {

    }];
}

它工作正常,但在更改单元格的框架后,我也可以点击我不想要的另一个单元格。我怎样才能做到这一点?

enter image description here

enter image description here

最佳答案

一种可能的解决方案是声明一个变量,该变量将保存像这样的扩展单元格的 indexPath 数组

// Will hold the indexPath of expanded cell
var expandedCell : [IndexPath] = []

第二件事是添加和删除展开/未展开的单元格,为此您必须更新您的 UITableView 委托(delegate) didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let index = expandedCell.firstIndex { (localIndexPath) -> Bool in
        return indexPath == localIndexPath
    }
    if let index = index  {
        expandedCell.remove(at: index)
    } else {
        expandedCell.append(indexPath)
    }
    tableviewMessageList.reloadRows(at: [indexPath], with: .fade)
} 

最后,您必须添加另一个 UITableView 委托(delegate) heightForRowAt 以返回单元格的高度,如果单元格的 indexPath 在数组中,它将返回扩展后的大小,否则返回单元格的正常大小像这样:-

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if expandedCell.contains(indexPath) {
        return tableView.frame.height
    }
    return 200.0 //NormalHeight
}

Note: My answer is in Swift but the same principle will apply for Objective-C you just need to change the Syntax.

关于ios - UITableViewCell 上的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55214770/

相关文章:

ios - 以编程方式创建的 UILabel 未出现在 IOS 7 Storyboard中

swift - Tableview 不显示我的附加数组,即使它们不为空

ios - React native xcode 调试和发布方案两种不同的构建

ios - 从重复使用的自定义单元格中的按钮传递数据

ios - 通话记录访问可能在 ios 8 中?

objective-c - 如何从 Objective-C 中的 NSString 中获取前 N 个单词?

iphone - 自定义单元的重复使用

ios - 更改 HeaderInSection 的 UITableView 高度?

ios - 如何根据 reuseIdentifier 控制我的单元格?

uitableview - Xcode 5 - iOS7 UITableView 更改