首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在用户单击UITableViewCellAccessoryDe​​tailDisclosureButton时显示选项

在用户单击UITableViewCellAccessoryDetailDisclosureButton时显示选项,可以通过以下步骤实现:

  1. 首先,在UITableViewDataSource协议中设置UITableViewCell的accessoryType为UITableViewCellAccessoryDetailDisclosureButton。
代码语言:swift
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "选项\(indexPath.row)"
    cell.accessoryType = .detailDisclosureButton
    return cell
}
  1. 接着,实现UITableViewDelegate协议中的didSelectRowAt方法,当用户单击UITableViewCell时,将选中的indexPath传递给另一个方法。
代码语言:swift
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    performSegue(withIdentifier: "showOptions", sender: indexPath)
}
  1. 最后,在prepare方法中,将选中的indexPath传递给目标视图控制器,以便在目标视图控制器中根据indexPath加载相应的选项。
代码语言:swift
复制
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let identifier = segue.identifier {
        if identifier == "showOptions" {
            if let destinationVC = segue.destination as? OptionsViewController, let indexPath = sender as? IndexPath {
                destinationVC.indexPath = indexPath
            }
        }
    }
}

在目标视图控制器中,根据indexPath加载相应的选项,并在视图加载时显示。

代码语言:swift
复制
class OptionsViewController: UIViewController {

    var indexPath: IndexPath?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let indexPath = indexPath {
            // 加载相应的选项
            // 显示选项
        }
    }
}

这样,当用户单击UITableViewCellAccessoryDetailDisclosureButton时,就会显示相应的选项。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 领券