简体   繁体   English

在Swift 4中单击自定义按钮时调整单元格的大小

[英]Resize cell when click on custom button in Swift 4

I have a view controller in which i have used table view controller in it. 我有一个视图控制器,我在其中使用了表视图控制器。 In my cell there is a button on which when user click the cell size should come to 550 and when click again it should come back to its original height. 在我的单元格中有一个按钮,当用户单击该单元格时,其大小应为550,再次单击时,应返回其原始高度。 I have tried bit code after searching for it but it isn't working is their any solution that can work for me?. 我在搜索后尝试了位代码,但是它们不起作用是否是对我有用的解决方案? My code is bit this, 我的代码就是这个

var indexOfCellToExpand: Int!

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if indexPath.row == indexOfCellToExpand {
    return 170 + expandedLabel.frame.height - 38
  }
  return 170
}

Swift 4.x 斯威夫特4.x

fileprivate var expandedIndexSet = Set<IndexPath>()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if expandedIndexSet.contains(indexPath) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_EXPANDED", for: indexPath) as! CellExpanded
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_COLLAPSED", for: indexPath) as! CellCollapsed
        return cell
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)
    if expandedIndexSet.contains(indexPath) {
        expandedIndexSet.remove(indexPath)
    } else {
        expandedIndexSet.insert(indexPath)
    }
    tableView.reloadRows(at: [indexPath], with: .fade)
}

Use Auto layout for Expand-collapse Cell, Attaching Demo for that case 将自动布局用于展开折叠单元格,在这种情况下附加演示

link:https: //www.dropbox.com/s/ieltq0honml35l8/TAbleDemo.zip?dl=0. 链接:https: //www.dropbox.com/s/ieltq0honml35l8/TAbleDemo.zip?dl = 0。

set cell button height constraint and update constraint on Value on select- deselect event and just reload data 设置单元格按钮高度约束并在select- deselect事件上更新Value约束,然后重新加载数据

in main viewcontroller code 在主ViewController代码中

self.tblView.estimatedRowHeight = 100
self.tblView.rowHeight = UITableViewAutomaticDimension

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TblCell", for: indexPath) as! TblCell

        cell.btnExpandCollepse.addTarget(self, action: #selector(ViewController.expandCollapse(sender:)), for: .touchUpInside)
        return cell

    }

 @objc func expandCollapse(sender:UIButton) {

        self.tblView.reloadData()
    }

Code in Cell Class 单元格中的代码

@IBOutlet var btnExpandCollepse: UIButton!
@IBOutlet var constraintBtnHeight: NSLayoutConstraint!

@IBAction func onExpandCollepse(_ sender: UIButton) {

        sender.isSelected = !sender.isSelected

        if !sender.isSelected{

            self.constraintBtnHeight.constant = 50
        }else{
            self.constraintBtnHeight.constant = 500
        }
    }

for constraint check below image http://prntscr.com/hur8ym 对于约束检查,请参见图片http://prntscr.com/hur8ym

Updated Demo for custom Content height of Lable with Autoresizing Cell https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0 更新了有关具有自动调整大小单元格的标签的自定义内容高度的演示 https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0

https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0 https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM