我的tableView遇到了一个非常奇怪的问题,有时你点击一个单元格并按应有的方式进行排序,但有时它会切换到随机的详细信息ViewController。我有 3 个从包含表视图的 UIViewController 连接:
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
}
}
}
}
地点对象具有放置按钮:[按钮]
三个 detailViewController 几乎相同,只是它们具有不同数量的按钮。
表视图根据位置的大小决定使用哪个 segue。buttons。
有时 tableView 像正常一样工作,有时它会传递随机单元格。不知道为什么。
简化prepare(for:
方法来解决此问题,如下所示:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
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 = self.selectedPlace
case SegueIdentifier.OneButton.rawValue:
assert(segue.destination.isKind(of: OneButtonViewController.self))
let vc = segue.destination as! OneButtonViewController
vc.place = self.selectedPlace
case SegueIdentifier.TwoButton.rawValue:
assert(segue.destination.isKind(of: TwoButtonViewController.self))
let vc = segue.destination as! TwoButtonViewController
vc.place = self.selectedPlace
default: break
}
}
似乎my_sections
是数组。在访问其项目之前,请尝试对其进行排序。我有一个简单的问题,当程序从持久存储中获取数据并且数据数组中的项目是无序的时。
我想出了问题所在,我很抱歉在这里花时间的人,我没有为任何人提供足够的信息来解决这个问题。
我的表视图从以下位置获取信息
var my_sections: [Section] = [] {
didSet {
return self.my_sections.sort(by: { $0.index < $1.index})
}
}
我的"部分"模型如下所示:
struct Section: Ordered {
var section: PTPlace.Section
var rows: [PTPlace]
var sorted_rows: [PTPlace] {
return self.rows.sorted(by: { $0.open_status.rawValue > $1.open_status.rawValue })
}
}
我注意到当行在部分内混淆而不是部分之间时,出现了一些问题。
所以我在方法中添加了以下行didSelectRowAtIndexPath
:
print("PLACE SELECTED: SECTION (indexPath.section) ROW
(indexPath.row) :: (my_sections[indexPath.section].rows[indexPath.row].name)")
我看到 tableView 的排列方式与行的排序方式大不相同,就好像有两个不同的数组一样。果然,这就是问题所在。
我的表视图在视图中显示sorted_rows数组,同时转到
my_sections[索引路径.部分]。rows[indexPath.row]而不是my_section[indexPath.section]。sorted_rows[索引路径.row]
问题解决了。愚蠢的错误。再次感谢所有帮助我简化代码的人。