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

标签 swift uicollectionview swift3 uicollectionviewcell

我在 HomeController (UICollectionView) 类和 PostCell (UICollectionViewCell) 类上有一个 Collection View 。 HomeController 只有 4 个不同的单元格。我的 PostCell 中也有一个按钮,当触摸该按钮时,它会处理 handleChangeSize 函数。当我触摸那个按钮时,我希望特定的单元格大小发生变化。最好改变高度,类似于 Instagram 在其单元格上“显示更多”功能。任何帮助将不胜感激。谢谢...这是我的代码:

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

最佳答案

我已经在 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 中选项按钮的 Action 是

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

关于swift - 点击按钮更改 UICollectionView 单元格大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761393/

相关文章:

ios - 在 Swift 中,无主与弱引用

ios - UICollectionViewCell 计数总是返回 0

ios - Swift 3.0 中的选择器

ios - Swift - 自定义 View 以显示空表格单元格

ios - Swift 中是否有 NSInvocation 的替代方案?

ios - 在 M1 上存档时 PhaseScriptExecution Embed Pod 失败

ios - 在代码中访问导航栏按钮

ios - 调用 Firebase Observer 后重新加载 TableView

ios - Uicollectionview 单元格宽度问题 iOS 10

ios - 将 'class variable' 分配给 'lazy var' 内的变量