我正在构建一个主要细节应用程序。我有一个截面 View Controller (VC),它是主 VC 的子项。单击 VC 部分中的行会显示不同的详细 VC,用户可以在其中填写所需信息。详细 VC 中的信息完成后,我希望在相关部分 VC 行中显示复选标记。仅供引用 selectedProject 是核心数据对象。
我在详细信息 VC viewWillDisappear 中调用通知来刷新部分 VC。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let detailCase: String = selectedProject!.section![indexPath.row]
configureCell(cell, withSectionName: detailCase)
print("location complete is \(selectedProject!.completeLocation)")
print("electricity complete is \(selectedProject!.completeElectricity)"
print(cell.textLabel!.text!)
print(cell.accessoryType.rawValue)
return cell
}
func configureCell(_ cell: UITableViewCell, withSectionName sectionName: String) {
switch sectionName {
case "Project Details" :
cell.accessoryType = .checkmark
case "Location" :
if selectedProject?.completeLocation == true {
cell.accessoryType = .checkmark
}
case "Electricity Use" :
if selectedProject?.completeElectricity == true {
cell.accessoryType = .checkmark
}
default :
cell.accessoryType = .none
}
cell.textLabel!.text = sectionName
}
func refreshTable(notification: Notification) -> Void {
print("Notification ran")
self.tableView.reloadData()
}
控制台:
通知运行
位置完整是真的
电完全是真的
项目详情
3
位置完整是真的
电完全是真的
位置
3
位置完整是真的
电完全是真的
用电
3
3 = .checkmark
所以看起来 cellForRowAt 确切地知道它应该做什么,但是表格并没有很容易地显示复选标记。相反,您要么必须单击返回主 VC,然后转到 VC 部分以显示复选标记,要么必须单击 VC 部分中的不同行,复选标记将随机显示。
我试过在 dispatchqueue.main.async 中调用 reloadData 但没有成功。我尝试在过程中的不同点再次调用 reloadData 但没有成功。我试过调用 tableView.beginUpdates/.endUpdates 和 reloadSectionData,但仍然没有成功显示复选标记。
如果我以编程方式连续提示 VC viewWilDisappear 和 viewWillAppear 部分,一旦选择了 VC 部分中的不同行,复选标记就会出现。它“似乎”像 reloadData() 没有提示完全重新加载 tableView。
仅供引用,附件项目在 Storyboard中设置为 .none。
最佳答案
需要在主队列上调用 UI 更新。试试这个:
DispatchQueue.main.async {
self.tableView.reloadData()
}
关于ios - Swift 3 tableView 不在 reloadData() 上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592375/