我的 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看起来像这样。
这是 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/