我正在构建一个主细节应用程序。我有一个部分视图控制器(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。
仅供参考,附件项目在情节提要中设置为 .none。