我有一个UITableViewController,它适用于TableViewCell:
类NewTableViewController:UITableViewController {
但是我想实现一个方法来更新和重新加载TableView中的数据。 更新部分运行良好,它删除CoreData对象,查询HealthKit,保存到CoreData,然后调用TableViewController中的一个函数(func setupArrays),它从CoreData获取数据并将其附加到cellForRowAtIndexPath中使用的数组。从控制台日志中我可以看到它运行良好(例如,更新的阵列反映了Healthkit中的更改)。当我尝试重新加载tableView时出现问题:
没有任何反应!self.tableView.reloadData()
我做了一些研究,并尝试了这种方法:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
仍然没有。
我在函数setupArrays的末尾调用reloadData函数。 (这也是我在日志中打印正确反映变化的数组的地方):
func setupArrays (){
if NSUserDefaults.standardUserDefaults().boolForKey("stepsSwitch") == true {
titleArray.append(stepsCell.title())
imageNameArray.append(stepsCell.imageName)
xAxisDatesArray.append(cdFetchSteps.queryCoreDataDate())
yAxisValuesArray.append(cdFetchSteps.queryCoreDataData())
}
if NSUserDefaults.standardUserDefaults().boolForKey("hrSwitch") == true {
titleArray.append(heartRateCell.title())
imageNameArray.append(heartRateCell.imageName)
xAxisDatesArray.append(cdFetchHeartRate.queryCoreDataDate())
yAxisValuesArray.append(cdFetchHeartRate.queryCoreDataData())
}
if NSUserDefaults.standardUserDefaults().boolForKey("weightSwitch") == true {
titleArray.append(weightCell.title())
imageNameArray.append(weightCell.imageName)
xAxisDatesArray.append(cdFetchWeight.queryCoreDataDate())
yAxisValuesArray.append(cdFetchWeight.queryCoreDataData())
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
println(yAxisValuesArray)
}
在IB中正确设置了delegate和dataSource。我试图明确添加#34;#34;在班上,例如:
类NewTableViewController:UITableViewController,UITableViewDataSource,UITableViewDelegate {
但那没有做任何事。
更新1
这是我的numberOfRowsInSection和cellForRowAtIndexPath:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var myCell:TableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as TableViewCell
myCell.title.text = titleArray[indexPath.row]
myCell.imageName = imageNameArray[indexPath.row]
myCell.xAxisDates = xAxisDatesArray[indexPath.row]
myCell.yAxisValues = yAxisValuesArray[indexPath.row]
return myCell }
更新2
当我设置断点时,cellForRowAtIndexPath会显示title和imageName的值,但在第一次加载和重新加载后,xAxisDates和xAxisValues都显示为空(请参阅附件picture)。这对我来说很奇怪,因为这些值在TableViewCell中可用并且显示正常。数组在调试区/变量视图中不可见吗?
问题:如何更新我的TableViewController?
非常欢迎任何帮助 - 谢谢。