点击按钮更改UICollectionView单元格大小?

时间:2017-01-20 10:29:52

标签: swift uicollectionview swift3 uicollectionviewcell

我的HomeController(UICollectionView)类和我的PostCell(UICollectionViewCell)类有一个集合视图。 HomeController只有4个不同的单元格。我的PostCell里面还有一个按钮,触摸后会处理handleChangeSize功能。当我触摸该按钮时,我希望特定的单元格大小发生变化。优选地改变高度,类似于Instagrams在其细胞上“显示更多”特征。任何帮助都将受到高度赞赏。谢谢......这是我的代码:

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

        let cellId = "cellId"

        override func viewDidLoad() {
            super.viewDidLoad()
            collectionView?.backgroundColor = UIColor(white: 0.90, alpha: 1.0)

            collectionView?.register(PostCell.self, forCellWithReuseIdentifier: cellId)
            collectionView?.showsVerticalScrollIndicator = false
            collectionView?.alwaysBounceVertical = true
        }

        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 4
        }

        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
            return cell
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let height = (view.frame.width) * 9 / 16
            return CGSize(width: view.frame.width, height: height + 50 + 50)
    }
}

class PostCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    lazy var myButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Change Size", for: .normal)

        button.addTarget(self, action: #selector(handleChangeSize), for: .touchUpInside)

        return button
    }()

    func handleChangeSize() {

    }



    func setupViews() {
        addSubview(myButton)

        myButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        myButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

2 个答案:

答案 0 :(得分:3)

我在tableView中完成了这个。对于collectionView也是如此。

var expandedCells = [Int]()

这是cellForRowAtIndexPath方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RequestTableViewCell") as! RequestTableViewCell
    cell.selectionStyle = .none
    cell.optionButton.tag = indexPath.row
    cell.optionButton?.addTarget(self, action: #selector(RequestsViewController.deleteAction), for: UIControlEvents.touchUpInside)
    return cell
  }

tableViewCell中的选项按钮操作为

func deleteAction(_ sender: UIButton){
    if let button = sender as? UIButton {
      // If the array contains the button that was pressed, then remove that button from the array
      if expandedCells.contains(sender.tag) {
        expandedCells = expandedCells.filter { $0 != sender.tag }
      }
        // Otherwise, add the button to the array
      else {
        expandedCells.removeAll()
        expandedCells.append(sender.tag)
      }
      // Reload the tableView data anytime a button is pressed
      self.requestTableView.beginUpdates()
      self.requestTableView.endUpdates()

    }
  }

并且heightForRowAtIndexPath方法是

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Set the row height based on whether or not the Int associated with that row is contained in the expandedCells array
    if expandedCells.contains(indexPath.row) {
      return 112
    } else {
      return 67
    }
  }

答案 1 :(得分:0)

这个问题的解决方案非常简单。首先,您需要指定要为特定单元格设置的高度,并在handleChangeSize()方法中执行该更改(这可以通过简单的frame.size.height = ...完成)。

在那之后,你可能需要调整整个集合视图的大小,否则,会有一些令人讨厌的惊喜。在调整单元格大小后执行计算(以获得新的高度)(您可以调用通知或在需要调整大小时触发的内容)。

相关问题