我试图在点击一个自定义tableViewHeader单元格中的UIButton后segue。我的代码:
自定义标头:
class MealsMealPlanHeaderTableViewCell: UITableViewHeaderFooterView {
@IBOutlet weak var mealLabel: UILabel!
@IBOutlet weak var mealCalories: UILabel!
@IBOutlet weak var editMeal: UIButton!
}
相关的ViewController代码:
class MealsMealPlanViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let sections = ["Breakfast", "Lunch", "Dinner", "Snacks"]
let foods = ["Oatmeal", "Sandwich", "Yogurt"]
let portions = ["160 calories, 1 cup", "200 calories, 1 serving", "80 calories, 1 cup"]
let headerId = "mealPlanHeader"
let cellId = "mealPlanCell"
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.register(UINib(nibName: "MealsMealPlanTableViewCell", bundle: nil), forCellReuseIdentifier: cellId)
tableView.register(UINib(nibName: "MealsMealPlanHeaderTableViewCell", bundle: nil), forHeaderFooterViewReuseIdentifier: headerId)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerId) as! MealsMealPlanHeaderTableViewCell
headerCell.mealLabel?.text = self.sections[section]
headerCell.mealCalories?.text = "400"
headerCell.editMeal.tag = section
headerCell.editMeal.addTarget(self, action: #selector(didPress(sender:)), for: UIControlEvents.touchUpInside)
return headerCell.contentView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MealsMealPlanTableViewCell
cell.foodLabel?.text = self.foods[indexPath.row]
cell.foodCaloriePortion?.text = self.portions[indexPath.row]
return cell
}
@IBAction func didPress(sender: AnyObject) {
print("hello")
self.performSegue(withIdentifier: "mealDetail", sender: sender)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mealDetail" {
print("made segue")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foods.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100
}
我定义了一个segue从问题中的ViewController到故事板中的ViewController目的地,并带有正确的标识符。我知道segue会导致崩溃因为当performSegue被移除时,只调用IBAction就可以了。有什么想法或替代解决方案吗?
编辑:我的错误显示如下:2016-10-27 16:15:11.105 Army H.E.A.L.T.H.[9344:478088] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Army_H_E_A_L_T_H_.MealsMealDetailViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc94f535bf0'
你的异常告诉你问题:"——[Army_H_E_A_L_T_H_。MealsMealDetailViewController tableView:numberOfRowsInSection:]:未识别的选择器发送到实例你必须设置MealsMealDetailViewController作为一些UITableView的数据源,但你实际上没有实现UITableViewDataSource协议。你的应用崩溃是因为它实例化了tableView, tableView向你的ViewController询问行数Objective C运行时抛出异常因为它找不到那个方法在你的ViewControllerer或它的任何超类上。