我想在我的自定义UITableViewCell上显示一个按钮,点击它就会将用户带到另一个屏幕。我试过以下代码,但它不起作用
儿童视图:
@IBAction func childScreenButton(sender: AnyObject) {
if let delegate = self.delegate {
delegate.childButtonClickedOnCell(self)
}
}
协议:
protocol childTableCellDelegate: class {
func childButtonClickedOnCell(cell: childViewCell)
}
父视图控制器:
func childButtonClickedOnCell(cell: FeedChildViewCell) {
self.clickedIndexPath = self.feedsTableView.indexPathForCell(cell)
self.performSegueWithIdentifier("toNextScreen", sender: self)
}
当我测试断点时,断点不会在子视图上进入"委托.儿童按钮点击单元格(self("。如果我在这里做错了什么,请告诉我。谢谢!!
我怀疑你有一些东西不合适,或者定义得不对。
我刚刚对此进行了快速测试,委托调用工作正常......看看你是否注意到任何不太一样的东西...
//
// TestTableViewController.swift
//
// Created by DonMag on 4/7/17.
// Copyright © 2017 DonMag. All rights reserved.
//
import UIKit
protocol MyCellDelegate {
func pressedButtonForMyCell(theSender: MyCell)
}
class MyCell: UITableViewCell {
@IBOutlet weak var theLabel: UILabel!
@IBOutlet weak var theButton: UIButton!
var delegate: MyCellDelegate?
@IBAction func childScreenButton(sender: AnyObject) {
delegate?.pressedButtonForMyCell(theSender: self)
}
}
class TestTableViewController: UITableViewController, MyCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell
cell.theLabel.text = "(indexPath)"
cell.delegate = self
return cell
}
func pressedButtonForMyCell(theSender: MyCell) {
print("delegate called", theSender)
}
}