[英]tableview:cellForRowAtIndexPath not getting called
I have a tableview which loads peripherals received from BLE. 我有一张表格视图,其中加载了从BLE接收的外围设备。 This
NSMutableArray
is maintained in the appDelegate. 此
NSMutableArray
在appDelegate中维护。 Whenever a new peripheral is received in the app I add it to the array. 每当在应用程序中收到新的外围设备时,我都会将其添加到阵列中。
My problem is that everything works fine on first run and the table updates accordingly and I use the app for a while. 我的问题是,首次运行时一切正常,表相应更新,我使用了一段时间。 Then I reset(CoreData, UserDefaults in app) my app and go back to the initial screen.
然后我重置(CoreData,应用程序中的UserDefaults)我的应用程序并返回到初始屏幕。
I use the following code to get back to initial screen of my 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()
}
When I reset app data and come back to the screen where I display the peripherals in the tableview, it doesn't updat. 当我重置应用程序数据并返回到我在表格视图中显示外围设备的屏幕时,它没有更新。 I can see that the peripherals are added in the array but when I reload the tableview
tableview:cellForRowAtIndexPath
is not called. 我可以看到外围设备已添加到数组中,但是当我重新加载tableview
tableview:cellForRowAtIndexPath
未被调用。 I tried to add a single dummy cell into the table and at that point tableview:cellForRowAtIndexPath
is called properly but only for the dummy cell. 我试图在表中添加一个虚拟单元格,此时
tableview:cellForRowAtIndexPath
被正确调用,但仅适用于虚拟单元格。
numberOfRowsInSection
returns the correct value and my datasource and delegate are correctly set too. numberOfRowsInSection
返回正确的值,我的数据源和委托也正确设置。 I clear the list every time a scan is started for BLE peripherals 每次启动BLE外设扫描时我都会清除列表
In AppDelegate: 在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)
}
}
In ScanVC: 在ScanVC中:
func discoveredPeripheral(peripheral: CAPeripheral) {
dispatch_async(dispatch_get_main_queue()) {
self.peripheralTableView.reloadData()
}
}
Hope I have explained well. 希望我能解释清楚。 Why is
tableview:cellForRowAtIndexPath
not called when I clearly have data to display? 当我显然有数据要显示时,为什么未调用
tableview:cellForRowAtIndexPath
?
If you say that you come back to initial viewController after cleaning CoreData and UserDefaults, I assume that it is why you don't see anything in your tableView, because you store found peripherals into CoreData and you clean it? 如果您说在清理CoreData和UserDefaults之后回到初始viewController,我认为这就是为什么您在tableView中看不到任何东西的原因,因为您将找到的外围设备存储在CoreData中并进行了清理? Call
discoveredPeripheral
when you come to initial controller. 当您进入初始控制器时,请致电
discoveredPeripheral
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.