我正在使用下面的代码来完成在分段控件中选择单元以执行segue并相应地传递信息后执行的操作,但是它没有按预期执行,在轻按该单元时它没有做任何事情。
以下代码通过扩展实现
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) -> UITableViewCell {
print("hello1")
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyWsPostCell
switch segmentControl.selectedSegmentIndex{
case 0:
print("hello2")
cell.section1 = section1list[indexPath.row]
cell.commentbutton.tag = indexPath.row
cell.commentbutton.addTarget(self, action: #selector(todetailview(_:)), for: .touchUpInside)
break
case 1:
print("hello4")
cell.section2 = section2list[indexPath.row]
cell.commentbutton.tag = indexPath.row
cell.commentbutton.addTarget(self, action: #selector(todetailview(_:)), for: .touchUpInside)
break
default:
break
}
return cell
}
调用的函数
@objc func todetailview(_ sender: AnyObject) {
performSegue(withIdentifier: "myWtoDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! DetailViewController
vc.infopassed = infokey
}
如果需要更多信息,请告诉我
Button
上点击cell
,可能因为didSelect
覆盖了Button
而未调用cell
。您可以使用更近一点。您的表视图单元格类别
class XYZ : UITableViewCell {
var btn1:(()->())?
//Create Action For Button
@IBAction func btnAction(_ sender: Any) {
btn1?()
}
}
在您的tableView中:cellForRowAt方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:..... cell.btn1 = { ()in // You can Perform your action here. No need to maintain any tag or index } }