【问题标题】:UITableView not updating on reloadDataUITableView 未在 reloadData 上更新
【发布时间】:2018-02-14 09:46:17
【问题描述】:

我在屏幕上有两个 tableview。我已经为两个 tableview 设置了委托和数据源。让我们考虑一个表格来显示主要过滤器和单击特定单元格/过滤器时,我必须在第二个表格视图中重新加载子过滤器。

所以我尝试了非常简单的解决方案,didSelectRowAt

subfilterTableView.reloadData()

即使我也尝试过调用主线程,

DispatchQueue.main.async { 
    subfilterTableView.reloadData()
}

它仍然没有重新加载subfilterTableView

我知道这个 beginUpdates() 和 endUpdates() 方法仅用于插入和删除单元格,但我在 beginUpdates() 和 endUpdates() 之间重新加载它会在被接受时崩溃。

我知道这是个愚蠢的问题,但我已经尝试了所有可能的更简单的解决方案。

以下是我遇到的一些情况:

  1. 有时数据会在第二次点击时填充
  2. 有时数据会在 3-5 秒后填充
  3. 刷新表格视图时也会填充数据

具有讽刺意味的是数据在真实设备上正确填充

以下是我的代码:

 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


【解决方案1】:

看来你的代码没有问题

只需在真机上运行即可,而不是在模拟器上运行

【讨论】:

  • 谢谢 是的,它在真实设备上工作,但我们必须找到确切的解决方案,为什么不在模拟器上工作。
  • 是的,你可以试试,但正如我在评论中所说的“它在模拟器中发生了一段时间,从 xcode 9.0 及更高版本开始”
【解决方案2】:

请检查您的第二个tableviewDatasourceDelegates

subfilterTableView.dataSource = self
subfilterTableView.delegate   = self

【讨论】:

  • 我已经提到我已经为两个表设置了数据源和委托
  • 在我看来,您的第二个 tableview 的委托有问题,您是否尝试过 didset 设置第二个 tableview 的数据源和委托。尝试在您的 tableview 插座中使用 didSet 设置数据源和委托,然后让我知道。
【解决方案3】:

输入viewDidLoad():

func viewDidLoad() {
    tableView.delegate = self
    tableView.datasource = self
}

【讨论】:

  • 我已经提到我已经为两个表设置了数据源和委托
  • @user7555810 你的问题没有具体说明你在哪里配置tableViews,所以在写问题时请尽可能具体,尽可能多地分享代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
相关资源
最近更新 更多