我有一个视图控制器,我在其中使用了表视图控制器。在我的单元格中有一个按钮,当用户单击该单元格时,其大小应为550,再次单击时,应返回其原始高度。我在搜索后尝试了位代码,但是它们不起作用是否是对我有用的解决方案?我的代码就是这样,
var indexOfCellToExpand: Int!
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == indexOfCellToExpand {
return 170 + expandedLabel.frame.height - 38
}
return 170
}
将自动布局用于展开折叠单元格,在这种情况下附加演示
链接:https://www.dropbox.com/s/ieltq0honml35l8/TAbleDemo.zip?dl = 0。
设置单元格按钮高度约束并在select- deselect事件上更新Value约束,然后重新加载数据
在主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()
}
单元类中的代码
@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
}
}
对于约束检查,请参见图片http://prntscr.com/hur8ym
更新了有关使用自动调整大小单元格的标签的自定义内容高度的演示 https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0
https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句