swift - Swift 当我使用 didSelectRowAt 时,Custom Cell 应该如何加载 tableview 数据并使其扩展?
问题描述
我创建了一个包含 tableview 的自定义单元格
这是 MainTableView 和数据
var data = [People(name:"Kevin",age:"18",tall:"180")]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainTableViewCell
cell.title = data[indexPath.row].name
cell.detail = ["age \(data[indexPath.row].age)","tall \(data[indexPath.row].tall)"]
cell.isExtend = false
return cell
}
我尝试点击单元格以扩展 tableView 高度并加载数据
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainTableViewCell
cell.isExpand = !cell.isExpand
}
这是 MainTableViewCell
class MainTableViewCell: UITableViewCell {
@IBOutlet weak var title: UILabel!
@IBOutlet weak var detailTableView: UITableView!
@IBOutlet weak var detailTableViewHeight: NSLayoutConstraint!
var detail:[String] = []{
didSet{
detailTableView.reloadData()
}
}
var isExpand: Bool = false{
didSet{
detailTableView.isHidden = !isExpand
detailTableView.reloadData()
detailTableViewHeight.constant = isExpand ? detailTableView.contentSize.height:0
}
}
override func awakeFromNib() {
super.awakeFromNib()
detailTableView.delegate = self
detailTableView.dataSource = self
detailTableView.isScrollEnabled = false
detailTableViewHeight.constant = 0
}
}
我使用 tableView 加载数据并首先隐藏一些主题,然后点击显示和隐藏主题
但什么也没发生
我忘了什么吗?
解决方案
在 cellforRowAt 中:
let cell: MoreUserDetails = tableView.dequeueReusableCell(withIdentifier: "MoreUserDetails") as! MoreUserDetails
cell.backgroundColor = .clear
cell.selectionStyle = .none
if isExpand {
// your func when its expanded
}
else {
// your func when its hidden
}
return cell
在没有选择
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if isSelected[indexPath.row] {
isExpand[indexPath.row] = false
}
else {
isExpand[indexPath.row] = true
}
self.tableVIew.reloadData()
}
推荐阅读
- html - 如何以角度增加垫子步进器的圆形图标的大小
- c - 在 VSCode 中使用 C/C++ 智能的 C 宏中没有代码自动完成
- powerbi - Power BI - 将行转换为列
- python - 如何关闭我在线程中使用的 Flask-SQLAlchemy 连接?
- regex - 正则表达式从字符串解析标签和值
- python - pathon POST 请求中的有效负载参数问题
- sql - 从过去的特定日期使用 DoB 计算年龄
- flutter - 如何处理颤振中的异常
- node.js - 无法使用路径模块构造正确的绝对路径
- javascript - 使用 react-inline-transition-group 在 SVG 而不是 DIV 上进行自定义转换