【问题标题】:(Automatic) reloadData on Tableview(tabbar2) once Array is updated in tabbar1 (without needing to change tabbars)(自动)在 tabbar1 中更新数组后,在 Tableview(tabbar2) 上重新加载数据(无需更改 tabbars)
【发布时间】:2020-11-08 16:35:24
【问题描述】:

tabbar1 - 我正在使用核心蓝牙连接到一些外围设备。连接后,我会更新一个数组。

 func populateFoundBTArray(peripheral: CBPeripheral, service:CBService) {
     discoveredBTDevices.append(BTPeripheral.init(
                               id: discoveredBTDevices.count, 
                               name: peripheral.name!, 
                               uuid: peripheral.identifier, 
                               desc: service.uuid.description
                               connected: peripheral.state == .connected ? true : false ))
    }

在 TabBar2 - 我有一些原型(自定义)单元格通过 XIB 文件通过静态单元格和动态单元格的组合连接起来(类似于此解决方案 - https://stackoverflow.com/a/49157374/14414215

在 TabBar2 内 - 我有 tableView.reloadData() 调用,这很棒,每当我切换到这个 tabbar2 时,tableview 都会更新。 (但我必须亲自前往 tabbar1 --> tabbar2 才能触发此更新)

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    tableView.reloadData()
  }

我的目标是希望能够在数组discoveredBTDevices 更新时重新加载表格视图。

我试过

let pop = tabbar2()
pop.tableView.reloadData() // also tried reloadSections(1 but didn't work

以及我在 SO 上找到的更多答案,但没有一个有效。 (我没有尝试任何转场/通知,因为目前我只是将发现的BTDevices 作为全局变量来简化我的初始测试)

【问题讨论】:

    标签: swift uitableview core-bluetooth


    【解决方案1】:

    关于我如何解决我的这个问题,我发布了我自己的答案。这不是一个完整的解决方案,而是一种解决方法,但我相信它是合适的。

    从原始帖子中,目标是在数组 discoveredBTDevices 更新后刷新 tableviewcell。回顾一下,目前表格只会在用户切换到 tabbar1 然后再回到 tabbar2 时刷新(由于在 viewDidAppear 中调用了 reloadData()

    UITableview 上搜索并阅读了 40 多个 SO 页面但没有找到答案后,我决定使用 UIButton 手动重新加载表格。

      // https://stackoverflow.com/a/53032365/14414215
      //
      // https://stackoverflow.com/a/44735567/14414215
      //
      override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        if section == deviceSection { // this is my paired device section of the static table
          let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 30.0))
          
          let sectionHeader = UILabel(frame: CGRect(x: 8, y: 0, width: 200, height: 30.0))
          sectionHeader.text = "PAIRED BLUETOOTH DEVICES"
          sectionHeader.font = UIFont.systemFont(ofSize: 12)
          sectionHeader.textColor = UIColor.systemGray
          
          let reloadButton = UIButton(frame: CGRect(x: tableView.frame.width-150, y: 0, width: 150, height: 30.0))
          reloadButton.setTitle("Reload", for: .normal)
          reloadButton.addTarget(self, action: #selector(ReloadButtonTapped), for: .touchUpInside)
          reloadButton.semanticContentAttribute = UIApplication.shared
              .userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
    
          headerView.addSubview(reloadButton)
          headerView.addSubview(sectionHeader)
          return headerView
        }
        return super.tableView(tableView, viewForHeaderInSection: section)
      }
    
    
      @objc func ReloadButtonTapped(_ sender : Any) {
        tableView.reloadData()
      }
    

    我已经链接了 2 个 SO 页面,其中显示了实现并稍微合并了它,因为使用 viewForHeaderInSection 将删除使用 Apple 原生标头的所有样式,因此添加了 sectionHeader

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2017-04-19
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多