ios - 使一个部分在其他部分的 Button 操作上可见
问题描述
我在 tableview 上显示多个部分的数据,
当我打开第 1 部分的 tableView 高度应该为 0。如果我单击第 0 部分中的按钮,第 1 部分的高度将为 150。例如,如果我单击(想要)部分单元格显示 150 的代理按钮高度,我单击生成器按钮部分高度将为 0
我想让第 1 部分在从第 0 部分单击的按钮上可见
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! postaddTableViewCell1
cell.ownerButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
cell.agentButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
cell.builderButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! postaddTableViewCell2
cell.checkbutton.tag = indexPath.row
cell.checkbutton.addTarget(self, action: #selector(checkbuttonClick) , for: .touchUpInside)
return cell
}
}
@objc func FisrtButtonClick(_ sender :UIButton)
{
let indexPath = IndexPath(row: 0, section: 0)
let cell = TableView.cellForRow(at: indexPath) as? postaddTableViewCell1
print(cell?.agentButton.titleLabel as Any)
if sender.tag == 10
{
cell?.ownerButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
cell?.agentButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.builderButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
}else if sender.tag == 11
{
cell?.ownerButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.agentButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
cell?.builderButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
} else if sender.tag == 12
{
cell?.ownerButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.agentButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.builderButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0
{
return 80
}
if indexPath.section == 1
{
return 0
}
return 80
}
解决方案
检查是否在方法中单击了所需的按钮FisrtButtonClick
并设置了boolean
可访问的heightForRowAt
. 切换布尔值后,您可以重新加载您需要的部分(也清理了您的代码 lil 位)。
var selectedAgent = false
@objc func firstButtonClick(_ sender: UIButton) {
if sender.currentTitle == "Agent" {
selectedAgent = true
tableView.reloadSections([1], with: .automatic)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1 && !selectedAgent {
return 0
}
return 80
}
推荐阅读
- excel - Excel VBA 数组公式使用索引匹配给出错误类型不匹配
- html - 有什么方法可以确保这个线性渐变上的所有条纹都是相同的大小?
- asp.net-mvc - Razor ASP.NET Core:为什么数据库连接状态在第一次加载时显示“正在连接”,然后在重新加载时成功连接到数据库?
- javascript - 使用 x-www-form-urlencoded content-type 将嵌套对象作为表单数据发布
- stata - 在 Stata 上将 Panel 数据从宽格式转换为长格式
- c# - C# HMACSHA256 给出的结果与 PHP hash_hmac 不同
- c++ - 防止 clang 格式在 return 和初始化列表之间放置一个空格
- api-platform.com - 模式生成器:需要几种类型的属性
- angular - 同一Angular组件中两个日期选择器控件上的多种日期格式
- 7zip - 使用 7zip 在拆分部分中归档时如何设置输出文件名