gpt4 book ai didi

ios - 展开和折叠表格 View 单元格

转载 作者:IT王子 更新时间:2023-10-29 05:10:12 26 4
gpt4 key购买 nike

我可以展开和折叠单元格,但我想调用 UITableViewCell 中的函数(展开和折叠)来更改按钮标题。

Iphone 5

import UIKitclass 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 UIKitclass 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)        })    }}

最佳答案

如果您希望单元格在物理上变得更大,那么在您存储 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

编辑

这将使单元格动画自己变大,你不需要单元格中的额外动画 block 。

编辑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()
}

关于ios - 展开和折叠表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36917030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com