ios - 单击切换按钮展开/折叠 UITableViewCell 高度

标签 ios swift uitableview

在 swift 3.0 中,我想通过单击位于 UITableViewCell 的切换按钮操作来展开/折叠我的 UITableViewCell 高度。

最佳答案

在此函数中,您必须为 UITableView 行提供不同的高度:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{

    if(selectedIndexPath == indexPath){
        return 200
    }
    return 60
}

注意:selectedIndexPath为待展开单元格的索引路径。

您必须像这样在展开按钮上重新加载选定的单元格:

tableView.reloadRowsAtIndexPaths([selectedIndexPath!], withRowAnimation: .Fade)

或者为了获得更好的动画效果,您可以使用以下函数代替 tableView.reloadRowsAtIndexPaths:

UIView.animateWithDuration(0.3, delay: 0.1, options: UIViewAnimationOptions.CurveEaseIn, animations: {

    self.tableView.beginUpdates()
    self.tableView.endUpdates()},completion:({ _ in
  }))

关于ios - 单击切换按钮展开/折叠 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712827/

相关文章:

iphone - viewController 中的 pushViewController 和 tableViewController

swift - 在 Xcode 中显示 SwiftLint 分析结果

ios - swift:将屏幕方向重置为纵向

ios - mvvmcross特定MvxViewController的ios锁定肖像方向

ios - 在 Xcode 7 的新 UI 自动化上模拟摇动手势的快速方法是什么?

ios - Swift - 如何根据整数中的索引位置从 UICollectionView 获取 UICollectionViewCell

ios - 在swift中使用字典的不同数据类型?

ios - 在 Popover tableview 中选择单元格后 UILabel 不刷新

ios - 将底部单元格添加到动态表格 View

ios - 绘图针未指向 MKMapView 上的确切位置