【发布时间】:2016-10-26 09:14:15
【问题描述】:
我创建新的 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
这是我的 MainViewController
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
}
}
}
【问题讨论】:
-
如何呈现第二个表格视图?包含它的视图是否允许用户交互?
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // 如果检查表的条件并给出它的返回值 }
-
在哪个控制器中,Didselect 方法不起作用?
-
我删除了一张表并留下了一张但仍然不起作用,我添加 self.view.isUserInteractionEnabled = true 也不能。我可以在 tableview 中选择行,当我点击时该行将显示为灰色,但不调用 didSelectRowAt indexPath
-
两个控制器都不工作
标签: ios iphone swift uitableview didselectrowatindexpath