【发布时间】:2016-08-23 08:34:21
【问题描述】:
我可以展开和折叠单元格,但我想在 UITableViewCell 中调用函数(展开和折叠)来更改按钮标题。
导入 UIKit 类 MyTicketsTableViewController: UITableViewController { var selectedIndexPath: NSIndexPath? var extraHeight: CGFloat = 100 覆盖 func viewDidLoad() { super.viewDidLoad() } 覆盖 func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // 处理所有可以重新创建的资源。 } 覆盖 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 返回 5 } 覆盖 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 让 cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTicketsTableViewCell 返回单元格 } 覆盖 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if(selectedIndexPath != nil && indexPath.compare(selectedIndexPath!) == NSComparisonResult.OrderedSame) { 返回 230 + 额外高度 } 返回 230.0 } 覆盖 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if(selectedIndexPath == indexPath) { selectedIndexPath = nil } 别的 { selectedIndexPath = indexPath } tableView.beginUpdates() tableView.endUpdates() } } 导入 UIKit 类 MyTicketsTableViewCell: UITableViewCell { @IBOutlet 弱 var expandButton:ExpandButton! @IBOutlet 弱变量 detailsHeightConstraint:NSLayoutConstraint! var defaultHeight: CGFloat! 覆盖 func awakeFromNib() { super.awakeFromNib() defaultHeight = detailsHeightConstraint.constant expandButton.button.setTitle("点击查看详细信息", forState: .Normal) detailsHeightConstraint.constant = 30 } 函数展开(){ UIView.animateWithDuration(0.3,延迟:0.0,选项:.CurveLinear,动画:{ self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.99)) self.detailsHeightConstraint.constant = self.defaultHeight self.layoutIfNeeded() },完成:{完成于 self.expandButton.button.setTitle("CLOSE", forState: .Normal) }) } 函数折叠(){ UIView.animateWithDuration(0.3,延迟:0.0,选项:.CurveLinear,动画:{ self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.0)) self.detailsHeightConstraint.constant = CGFloat(30.0) self.layoutIfNeeded() },完成:{完成于 self.expandButton.button.setTitle("点击查看详细信息", forState: .Normal) }) } }【问题讨论】:
-
请查看此解决方案:enter link description here
-
Swift 4.2 我以编程方式创建了一个示例可扩展表格视图。 - 扩展单元格出现和消失的动画。 - 所选部分的标题在展开时更改。 - 视图在代码中,不需要故事板。在下面的链接中查看:???? Gist of expandable table view
标签: ios swift uitableview