ios - 我正在制作 list ,是否可以使用 Segue 选择和取消选择

标签 ios swift xcode uitableview

我的 UITableView 中有一个 list ,我还有一个链接单元格的 segue。是否可以在同一个单元格中选择/取消选择和一个 segue。因为当我单击复选框时,它会选择并转到另一个 VC。就像两者的单独选择区域一样。

override func viewDidLoad() {

    super.viewDidLoad()

    [tableView .setEditing(true, animated: true)]
 }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        performSegue(withIdentifier: "details", sender: self)
        tableView.deselectRow(at: indexPath, animated: true)
        NSLog("user selected", list)
   }

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    NSLog("user de-selected", list)
}


override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle(rawValue: 3)!
}

最佳答案

这是我想说的例子。
我的 Storyboard看起来像这样。 Storyboard
这是 ViewController 类的代码。
从那里我正在设置按钮的目标方法和按钮的标签。
从按钮的选择器获取该标签并传递给 perfromSegue 并从该传递给 NextViewController

class ViewController: UIViewController {
    func nextButtonClicked(_ sender: UIButton) {
        let index = sender.tag
        performSegue(withIdentifier: "goToNextVC", sender: index)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToNextVC" {
            let index = sender as! Int
            let vc = segue.destination as! NextViewController
            vc.someValue = index
        }
    }
}
extension UIViewController: UITableViewDelegate, UITableViewDataSource {
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 15
    }
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myTableCell", for: indexPath) as! MyTableViewCell

        cell.lblTitle.text = "Row " + String(indexPath.row)
        cell.nextButton.tag = indexPath.row
        cell.nextButton.addTarget(self, action: #selector(ViewController.nextButtonClicked(_:)), for: .touchUpInside)

        return cell
    }
}

下面是NextViewController

的代码
class NextViewController: UIViewController {
    var someValue:Int!
    @IBOutlet weak var lblComingFrom: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        if someValue != nil {
            lblComingFrom.text = "I'm loaded via selecting row at index \(someValue!) "
        }
    }
}

希望这对你有帮助...

关于ios - 我正在制作 list ,是否可以使用 Segue 选择和取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811366/

相关文章:

ios - iCarousel 自定义 View xib 自动更改 subview 宽度

ios - 在 iOS 上哪里执行 UI 元素样式

ios - 从另一个 Controller 更新 UI

swift - 扫描真实世界对象并从中生成 3D 网格

ios - EXC_BAD_INSTRUCTION 运行时错误

ios - Xcode 5 不显示在 iOS 6 上运行模拟器的方案选项

ios - 我的应用程序在 WhatsApp 的共享扩展中

IOS - 将消息添加到状态栏

ios - 如何在CloudKit中获取用户记录?

ios - React-Native:为什么在 iOS 设备上测试时会出现网络错误