Ich gehe schon seit einiger Zeit im Kreis und nichts, was ich in ähnlichen Beiträgen gefunden habe, scheint es zu lösen. Ich programmiere programmatisch eine Tabelle zu einer benutzerdefinierten UIView. Der Tabellen- und Zeilentext wird korrekt angezeigt, aber weder didSelectRowAtIndexPath noch willdSelectRowAtIndexPath werden ausgelöst, wenn ich dies im Simulator ausführe und versuche, auf eine der Zeilen zu klicken.didSelectRowAtIndexPath wird nicht aufgerufen (Swift)
Die entsprechenden Bits meines Codes unter:
import UIKit
import AVFoundation
@IBDesignable
class PerformanceQuestionView: UIView, UITableViewDelegate, UITableViewDataSource {
var optionsTable = UITableView(frame: CGRectMake(10,200,250,200))
var optionItems = ["One", "Two", "Three", "Four"]
convenience init(rect: CGRect, performanceQuestion:PerformanceQuestion?) {
self.init(frame: rect)
NSLog("PerformanceQuestionView.init()")
self.optionsTable.dataSource = self
self.optionsTable.delegate = self
self.optionsTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.optionsTable.allowsSelection = true
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
NSLog("numberOfRowsInSection")
return optionItems.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
NSLog("cellForRowAtIndexPath")
var cell:UITableViewCell = self.optionsTable.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel.text = self.optionItems[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, willSelectRowAtIndexPath indexPath: NSIndexPath!) -> NSIndexPath! {
NSLog("You will select cell #\(indexPath.row)!")
return indexPath
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
NSLog("You selected cell #\(indexPath.row)!")
}
override func layoutSubviews() {
super.layoutSubviews()
self.addSubview(optionsTable)
}
}
verbinden Sie Ihre Tableview-Datenquelle und Delegate von Storyboards.? –
@NitinGohel Sie sind in der Init-Methode verbunden. –
bettero zum Überprüfen einmal verbinden von storyBoard –