简体   繁体   中英

tableview:cellForRowAtIndexPath not getting called

I have a tableview which loads peripherals received from BLE. This NSMutableArray is maintained in the 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.

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. 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.

numberOfRowsInSection returns the correct value and my datasource and delegate are correctly set too. I clear the list every time a scan is started for BLE peripherals

In 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:

   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?

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? Call discoveredPeripheral when you come to initial controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM