我创建了新的 ViewController 并拖动了新的 UITableView 它可以工作,但是在我之前的两个 ViewController 中无法工作。
以下所有我已经完成的:
- UITableViewDelegate .delegate=self
- UITableViewDataSource .datasource=self
- Swift3 并且是 didSelectRowAt 而不是 didDeselectRowAt
- .setEditing(true, animated: true)
- .allowsSelection = true
- .allowsSelectionDuringEditing = true
- 单一选择
我所有的数据都可以加载到tableview中,唯一的问题是不能选择行,在MainViewController中实际上我只有一个table view,另一个用于测试。
- 这是我的 tableview 无法选择行但可以加载数据的 viewcontroller 之一,这里我只有一个 tableview,但为了测试我创建了一个新的 table view
这是我的主视图 Controller
extension MainViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableview_restaurants {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RestaurantsCell
cell.label_name.text = "OMG Restaurant # \(String(indexPath.row))"
return cell
}
else if tableView == self.tableview_test {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableviewtestCell
cell.label_test.text = String(indexPath.row)
return cell
}
else {
let cell = UITableViewCell()
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//tableView.deselectRow(at: indexPath, animated: true)
if tableView == self.tableview_restaurants {
}
else if tableView == self.tableview_test {
print("select this row")
print(indexPath)
}
}
}
这是我的 FilterViewController
extension SearchFilterViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var numberofrowsinsecion:Int = 0
if (tableView == self.tableview_quickfilters) {
numberofrowsinsecion = json_quickfilters["filters"].arrayValue.count
//print(numberofrowsinsecion)
}
else if (tableView == self.tableview_cuisinetype) {
numberofrowsinsecion = json_cuisinetype["cuisine_types"].arrayValue.count
//print(numberofrowsinsecion)
}
return numberofrowsinsecion
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (tableView == self.tableview_quickfilters) {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! QuickFiltersCell
let filter = json_quickfilters["filters"].arrayValue
if let filter_name = cell.label_quickfiltersname {
filter_name.text = filter[indexPath.row]["name"].stringValue
filter_name.font = Common.Config.FontSize.XL_L
}
return cell
}
else if (tableView == self.tableview_cuisinetype) {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CuisineTypeCell
let cuisinetype = json_cuisinetype["cuisine_types"].arrayValue
if let cuisinetype_name = cell.label_cuisinetypename {
cuisinetype_name.text = cuisinetype[indexPath.row]["name"].stringValue
cuisinetype_name.font = Common.Config.FontSize.XL_L
}
return cell
}
else {
let cell = UITableViewCell()
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (tableView == self.tableview_quickfilters) {
print(indexPath)
//let cell = tableView.cellForRow(at: indexPath) as! QuickFiltersCell
} else if (tableView == self.tableview_cuisinetype) {
print(indexPath)
//let cell = tableView.cellForRow(at: indexPath) as! CuisineTypeCell
}
}
}
最佳答案
您很可能在某处添加了一个点击手势来取消单元格点击。在这里查看答案:https://stackoverflow.com/a/9248827/1286291
关于ios - 自定义单元格中未调用 UITableView didSelectRowAt indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40258226/