我能够扩展和折叠单元格,但我想在UITableViewCell中调用函数(展开和折叠)来更改按钮标题。
import UIKit class MyTicketsTableViewController: UITableViewController { var selectedIndexPath: NSIndexPath? var extraHeight: CGFloat = 100 override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTicketsTableViewCell return cell } override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if(selectedIndexPath != nil && indexPath.compare(selectedIndexPath!) == NSComparisonResult.OrderedSame) { return 230 + extraHeight } return 230.0 } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if(selectedIndexPath == indexPath) { selectedIndexPath = nil } else { selectedIndexPath = indexPath } tableView.beginUpdates() tableView.endUpdates() } }
import UIKit class MyTicketsTableViewCell: UITableViewCell { @IBOutlet weak var expandButton: ExpandButton! @IBOutlet weak var detailsHeightConstraint: NSLayoutConstraint! var defaultHeight: CGFloat! override func awakeFromNib() { super.awakeFromNib() defaultHeight = detailsHeightConstraint.constant expandButton.button.setTitle("TAP FOR DETAILS", forState: .Normal) detailsHeightConstraint.constant = 30 } func expand() { UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: { self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.99)) self.detailsHeightConstraint.constant = self.defaultHeight self.layoutIfNeeded() }, completion: { finished in self.expandButton.button.setTitle("CLOSE", forState: .Normal) }) } func collapse() { UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: { self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.0)) self.detailsHeightConstraint.constant = CGFloat(30.0) self.layoutIfNeeded() }, completion: { finished in self.expandButton.button.setTitle("TAP FOR DETAILS", forState: .Normal) }) } }
答案 0 :(得分:24)
如果您希望细胞体积更大,那么您在商店IndexPath
的位置,heightForRow:
使用:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if selectedIndexPath == indexPath {
return 230 + extraHeight
}
return 230.0
}
然后当你想在didSelectRow中展开一个:
selectedIndexPath = indexPath
tableView.beginUpdates
tableView.endUpdates
修改强>
这将使细胞自身变大,你不需要细胞中额外的动画块。
修改2
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(selectedIndexPath == indexPath) {
selectedIndexPath = nil
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell {
cell.collapse()
}
if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell {
cell.collapse()
}
} else {
selectedIndexPath = indexPath
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell {
cell.expand()
}
if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell {
cell.expand()
}
}
tableView.beginUpdates()
tableView.endUpdates()
}
答案 1 :(得分:2)
您只需要以这种方式实现UITableView Delegate:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
默认情况下,estimatedHeight是CGRectZero,当你为它设置一些值时,它会启用自动调整(与UICollectionView相同的情况),你甚至可以这样做:
tableView?.estimatedRowHeight = CGSizeMake(50.f, 50.f);
然后你需要在细胞内设置约束。
希望有所帮助)
答案 2 :(得分:1)
在MyTicketsTableViewController
类中,在cellForRowAtIndexPath
数据源方法内添加按钮的目标。
cell.expandButton.addTarget(self, action: "expandorcollapsed:", forControlEvents: UIControlEvents.TouchUpInside)
答案 3 :(得分:0)
我试图在此页面以及其他页面上实现许多给定示例的问题,但是对我没有用,因为我必须在自定义单元格中执行一些逻辑(例如,当单元格隐藏在CustomCell.swift中不需要的UILables时)已折叠)。
以下是对我有用的实现:
创建字典:
private var _expandedCells: [IndexPath:Bool] = [:]
实施heightForRowAt方法:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return _expandedCells[indexPath] == nil ? 70 : _expandedCells[indexPath]! ? 150 : 70
}
实施didSelectRowAt方法:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
_expandedCells[indexPath] = _expandedCells[indexPath] == nil ? true : !_expandedCells[indexPath]!
tableView.reloadRows(at: [indexPath], with: .fade)
}
调整您的customCell:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = cell as? YourCustomTableViewCell, let isExpanded = _expandedCells[indexPath] else { return }
cell.set(expanded: isExpanded)
}