【问题标题】:tableview:cellForRowAtIndexPath not getting calledtableview:cellForRowAtIndexPath 没有被调用
【发布时间】:2017-11-24 12:37:57
【问题描述】:

我有一个表格视图,可以加载从 BLE 接收的外围设备。 thisNSMutableArray 在 appDelegate 中维护。每当在应用程序中收到新的外围设备时,我都会将其添加到数组中。

我的问题是,在第一次运行时一切正常,表格也会相应更新,我使用该应用程序一段时间。然后我重置(CoreData, UserDefaults in app) 我的应用程序并返回初始屏幕。

我使用以下代码返回应用程序的初始屏幕

class func showStartScreen() {
    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    let nav = storyboard.instantiateInitialViewController()
    (UIApplication.sharedApplication().delegate)!.window!!.rootViewController = nav
    (UIApplication.sharedApplication().delegate)!.window!!.makeKeyAndVisible()
}

当我重置应用数据并返回到我在表格视图中显示外围设备的屏幕时,它不会更新。我可以看到外围设备已添加到数组中,但是当我重新加载 tableview tableview:cellForRowAtIndexPath 时没有调用。 我尝试在表格中添加一个虚拟单元格,此时 tableview:cellForRowAtIndexPath 被正确调用,但仅适用于虚拟单元格。

numberOfRowsInSection 返回正确的值,并且我的数据源和委托也设置正确。 每次开始扫描 BLE 外围设备时,我都会清除列表

在 AppDelegate 中:

func discoveredPeripheral(peripheral: CAPeripheral) {
    print("added peripheral")
    var isDuplicate:Bool = false
    for object in scannedPeripherals!  {
        if object is CAPeripheral {
            let newPeripheral = object as! CAPeripheral
            // Need to add check of nil
            if newPeripheral.peripheral_UUID == peripheral.peripheral_UUID {
                isDuplicate = true
            }
        }
    }
    if (!isDuplicate) {
        self.scannedPeripherals?.addObject(peripheral)
    }
}

在 ScanVC 中:

   func discoveredPeripheral(peripheral: CAPeripheral) {

    dispatch_async(dispatch_get_main_queue()) {
        self.peripheralTableView.reloadData()
    }
}

希望我已经解释清楚了。为什么当我清楚地有数据要显示时没有调用tableview:cellForRowAtIndexPath

【问题讨论】:

  • 确定你设置了tableView的委托和数据源吗?
  • 您要重新加载表格吗?请发布适当的sn-ps代码。
  • @mikep 添加了用于添加外围设备和重新加载数据的代码
  • @iPatel 是的,我已经检查了数据源和委托。它们设置正确。
  • 确保您也看到了cellForRowAtIndexPath: not called

标签: ios uitableview reloaddata swift2.3


【解决方案1】:

如果你说你在清理 CoreData 和 UserDefaults 之后回到初始 viewController,我认为这就是为什么你在 tableView 中看不到任何东西,因为你将找到的外围设备存储到 CoreData 并清理它?当您来到初始控制器时,请致电discoveredPeripheral

【讨论】:

  • 我不在核心数据中存储外围设备。每次加载我的 ScanVC 时,我都会进行新的搜索。我已经提到我收到了数据,但是在重新加载表格视图时没有调用 cellForRowAtIndexPath。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多