首页 文章

展开和折叠tableview单元格

提问于
浏览
15

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

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

}

3 回答

  • 2

    MyTicketsTableViewController 类中,在 cellForRowAtIndexPath 数据源方法内添加按钮的目标 .

    cell.expandButton.addTarget(self, action: "expandorcollapsed:", forControlEvents: UIControlEvents.TouchUpInside)
    
  • 18

    您只需要以这种方式实现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);
    

    然后,您需要在单元格内设置约束 .

    查看这篇文章:https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-ns

    希望能帮助到你)

  • 0

    如果你想让细胞变得更大,那么你的商店 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
    

    Edit

    这将使细胞自身变大,你不需要细胞中额外的动画块 .

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

相关问题