ios - 删除按钮和行之间不需要的空间 swift xcode
问题描述
我是 iPhone 应用程序开发的新手。
我正在设计这样的应用程序。在这个设计中,下面有两个按钮用于删除按钮,在这两个按钮的下方,有线条。你在这张照片中看到的。
我想要的是,当这两个按钮被某些条件隐藏时,我想将下面的行添加到删除按钮。如果这两个按钮可见意味着,那条线应该在这两个按钮的下方。不在删除按钮下方。
我在 TableView 的 cellForRowAt 函数中添加了以下代码。它不工作。
cell.line.frame = CGRect(x: 0, y:cell.btnDelete.frame.maxY, width: cell.line.frame.width , height: 1 )
任何机构都可以帮助我解决这个问题。
我添加了以下代码。
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
和
dataTable.rowHeight = UITableViewAutomaticDimension
dataTable.estimatedRowHeight = 100
解决方案
您必须使用一些棘手的方法来实现这一点。使用堆栈视图添加显示,隐藏按钮到一个 UIView。将所有其他组件添加到另一个 UIView。将 UIStackView 添加到您的自定义单元格并添加这样的约束。
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
将 stackView 分布设置为 .Fill。
当您想要显示/隐藏按钮时,您可以从 tableview 单元 stackView 添加/删除排列的子视图。如果您在cellForItemAt中显示/隐藏按钮,请确保在添加之前删除所有排列的子视图。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell.stackView.removeArrangedSubview(cell.viewOne)
cell.stackView.removeArrangedSubview(cell.viewTwo)
if isShowBuutons {
cell.stackView.addArrangedSubview(cell.viewOne) //View with All other elements
cell.stackView.addArrangedSubview(cell.viewTwo) //View with buttons
}else{
cell.stackView.addArrangedSubview(cell.viewOne) //View with All other elements
}
}
推荐阅读
- snowflake-cloud-data-platform - 在雪花存储过程中处理geojson数据
- python - 从数组中挑选元素
- python - 环境和虚拟环境有什么区别?
- elasticsearch - 如何使用多行将文本日志文件导入 Elasticsearch 本地安装
- redux - 我的 redux 状态发生了变化,但视图没有
- c# - NET 5 中的 RemotingServices 和 GC
- python - 使用 python 扩展 gdb:ModuleNotFoundError
- node.js - AWS Lambda node.js 'ping' 模块权限错误
- php - intercom-php API - 使用电子邮件创建对话返回 400 错误请求
- c# - 如何在编辑器中为 Unity 中的旋转平台脚本添加列表