【发布时间】:2018-03-18 17:29:46
【问题描述】:
我的 tableView 有一个非常奇怪的问题,有时您单击一个单元格并按照它应有的方式进行切换,但其他时候它会切换到一个随机的 detailViewController。 我有 3 个从包含 tableview 的 UIViewController 连接的 segue:
segues 以模态方式“呈现”detailViewController 并将自定义对象“place”传递给 detailViewController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("PLACE SELECTED: SECTION \(indexPath.section) ROW \(indexPath.row) :: \(my_sections[indexPath.section])")
self.selectedPlace = my_sections[indexPath.section].rows[indexPath.row]
let buttons_count = self.selectedPlace!.buttons.count
switch buttons_count {
case 0:
self.performSegue(withIdentifier: SegueIdentifier.NoButton.rawValue, sender: self.tableview.cellForRow(at: indexPath))
case 1:
self.performSegue(withIdentifier: SegueIdentifier.OneButton.rawValue, sender: self.tableview.cellForRow(at: indexPath))
case 2:
self.performSegue(withIdentifier: SegueIdentifier.TwoButton.rawValue, sender: self.tableview.cellForRow(at: indexPath))
default:
break
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (sender as? PlaceTableViewCell) != nil {
if let indexPath = self.tableview.indexPathForSelectedRow {
let place = self.my_sections[indexPath.section].rows[indexPath.row]
navigationController?.view.backgroundColor = UIColor.clear
switch(segue.identifier!) {
case SegueIdentifier.NoButton.rawValue:
assert(segue.destination.isKind(of: PlaceDetailsViewController.self))
let vc = segue.destination as! PlaceDetailsViewController
vc.place = place
case SegueIdentifier.OneButton.rawValue:
assert(segue.destination.isKind(of: OneButtonViewController.self))
let vc = segue.destination as! OneButtonViewController
vc.place = place
case SegueIdentifier.TwoButton.rawValue:
assert(segue.destination.isKind(of: TwoButtonViewController.self))
let vc = segue.destination as! TwoButtonViewController
vc.place = place
default: break
}
}
}
}
place 对象有 place.buttons: [Button]
三个 detailViewController 几乎相同,只是按钮数量不同。
tableView 根据 place.buttons 的大小决定使用哪个 segue
有时 tableView 像正常一样工作,有时它会传递随机单元格。不知道为什么。
【问题讨论】:
-
在
prepare(for:)为什么又用选中的单元格来获取位置呢?它已经在self.selectedPlace中,或者您可以简单地将位置作为sender参数提供给performSegue。 -
好的,在遵循这里给出的大部分建议之后,我认为这是一个排序错误。不知何故 my_sections.rows 与表中列出的顺序不匹配!
标签: ios swift uitableview