如何在UITableViewCell渲染后重新调整大小?

时间:2015-12-17 22:54:11

标签: ios uitableview

我想以编程方式调整UITableViewCell已经渲染后的高度。由于单元格已经渲染,我无法使用tableView:heightForRowAtIndexPath:

如何在已经渲染后重新调整单元格的大小?

1 个答案:

答案 0 :(得分:3)

您必须在tableView:heightForRowAtIndexPath:中重新计算,之后您必须重新加载此单元格或致电tableView reloadData

您希望在第4行调整单元格大小的示例。您可以点击按钮来调整大小,您可以像这样实现

func resizeCell() {
     self.indexReload = NSIndexPath(section: 0, row:4)
     self.heightYouWant = heightYouWant
     self.tableView.reloadRowsAtIndexPaths([self.indexReload], withRowAnimation: .Fade)
}

heightForCellAtIndexPath你必须这样实现:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
     if index == self.indexReload {
         return self.heightYouWant
     } or {
         //calculate normal as you want
     }
}

这是我的想法。希望这有帮助!