【发布时间】:2018-02-14 09:46:17
【问题描述】:
我在屏幕上有两个 tableview。我已经为两个 tableview 设置了委托和数据源。让我们考虑一个表格来显示主要过滤器和单击特定单元格/过滤器时,我必须在第二个表格视图中重新加载子过滤器。
所以我尝试了非常简单的解决方案,didSelectRowAt,
subfilterTableView.reloadData()
即使我也尝试过调用主线程,
DispatchQueue.main.async {
subfilterTableView.reloadData()
}
它仍然没有重新加载subfilterTableView。
我知道这个 beginUpdates() 和 endUpdates() 方法仅用于插入和删除单元格,但我在 beginUpdates() 和 endUpdates() 之间重新加载它会在被接受时崩溃。
我知道这是个愚蠢的问题,但我已经尝试了所有可能的更简单的解决方案。
以下是我遇到的一些情况:
- 有时数据会在第二次点击时填充
- 有时数据会在 3-5 秒后填充
- 刷新表格视图时也会填充数据
具有讽刺意味的是数据在真实设备上正确填充
以下是我的代码:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 1 {
return filters.count
}
else{
return subFilters.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 1 {
let filter = filters[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "FilterTableViewCell", for: indexPath) as! FilterTableViewCell
cell.labelFilter.text = filter["filterName"] as? String
cell.backgroundColor = UIColor.clear
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
cell.selectionStyle = .none
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "SubFilterTableViewCell", for: indexPath) as! SubFilterTableViewCell
cell.labelSubfilter.text = subFilters[indexPath.row]
cell.backgroundColor = UIColor.clear
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.tag == 1 {
let selectedCell:FilterTableViewCell = tableView.cellForRow(at: indexPath)! as! FilterTableViewCell
selectedCell.labelFilter.textColor = UIColor.black
selectedCell.contentView.backgroundColor = UIColor.white
subFilters = filters[indexPath.row]["subFilterValue"] as! [String]
self.tableViewSubfilters.reloadData()
// DispatchQueue.main.async {
// self.tableViewSubfilters.reloadData()
// }
// tableViewSubfilters.performSelector(onMainThread: #selector(self.reloadData), with: nil, waitUntilDone: true)
}
}
【问题讨论】:
-
你是如何过滤数据的,问题可能是数据没有被过滤,并且子表显示的数据与重新加载前相同。
-
没有人我做对了,即使我的 cellForRowAtIndexPath 也完美地调用了我返回的单元格数量,但数据没有立即填充,有时它会在 3-4 秒后填充,而我不是使用任何计时器,不执行另一个任何执行。
-
你可以在这里展示你的课程吗?
-
你能分享更多你的代码吗?表是如何初始化的,您是否设置了正确的节数和行数?
didSelectRowAt被调用了吗?你有什么办法解决的吗? -
@user7555810 :从 xcode 9.0 及更高版本开始,它在模拟器中发生了一段时间
标签: ios swift uitableview