展开和折叠tableview单元格 [英] Expand and Collapse tableview cells

查看:152
本文介绍了展开和折叠tableview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm able to expand and collapse cells but i wanna call functions (expand and collapse) inside UITableViewCell to change button title.


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

}


推荐答案

如果您希望单元格变大,那么您的商店就在哪里 IndexPath ,在 heightForRow:使用:

If you want the cell to get physically bigger, then where you have your store IndexPath, in heightForRow: use:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if selectedIndexPath == indexPath {
      return 230 + extraHeight
    }
    return 230.0
}

然后当你想在didSelectRow中扩展一个时:

Then when you want to expand one in the didSelectRow:

selectedIndexPath = indexPath
tableView.beginUpdates
tableView.endUpdates

编辑

这将使细胞动画变得更大,你不需要细胞中额外的动画块。

This will make the cells animate themselves getting bigger, you dont need the extra animation blocks in the cell.

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

这篇关于展开和折叠tableview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆