【发布时间】: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 是的,我已经检查了数据源和委托。它们设置正确。
标签: ios uitableview reloaddata swift2.3