Ios Swift 3 tableView在重新加载数据时未更新()

Ios Swift 3 tableView在重新加载数据时未更新(),ios,swift,xcode,uitableview,checkmark,Ios,Swift,Xcode,Uitableview,Checkmark,我正在构建一个主细节应用程序。我有一个截面视图控制器(VC),它是主VC的子级。单击VC部分中的行会显示不同的详细信息VCs,用户可以在其中完成所需的信息。完成详细信息VC后,我希望在相关章节VC行中显示复选标记。仅供参考selectedProject是一个核心数据对象 我在详细信息VC视图中调用通知将消失以刷新VC部分 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

我正在构建一个主细节应用程序。我有一个截面视图控制器(VC),它是主VC的子级。单击VC部分中的行会显示不同的详细信息VCs,用户可以在其中完成所需的信息。完成详细信息VC后,我希望在相关章节VC行中显示复选标记。仅供参考selectedProject是一个核心数据对象

我在详细信息VC视图中调用通知将消失以刷新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的=.checkmark

因此,cellForRowAt似乎确切地知道它应该做什么,但表并不容易显示复选标记。取而代之的是,你要么点击返回到主VC,然后转发到VC部分,以显示复选标记,要么点击VC部分的不同行,复选标记将随机显示

我尝试在dispatchqueue.main.async中调用reloadData,但没有成功。我再次尝试在过程中的不同点调用reloadData,但没有成功。我尝试调用tableView.BeginUpdate/.endUpdates并重新加载SectionData,但还是没有成功地显示复选标记

如果我以编程方式提示section VC viewWilDisappear,并且viewWilDisappear将连续出现,则选中section VC中的另一行后,复选标记将出现。似乎reloadData()并没有提示完全重新加载tableView


FYI附件项在情节提要中设置为.none。

需要在主队列上调用UI更新。试试这个:

DispatchQueue.main.async {
    self.tableView.reloadData()
}

好的,鉴于没有即将到来的修复,我将tableView更改为静态而不是动态。我为所有固定单元格创建了tableViewCell插座,然后以这种方式添加和删除复选标记

任何感兴趣的人的代码:

var tableCellArray = [UITableViewCell]()

/*-----------------------------------------------------------*/

// MARK: - Outlets

@IBOutlet weak var projectDetails: UITableViewCell!
@IBOutlet weak var location: UITableViewCell!
@IBOutlet weak var electricityUse: UITableViewCell!

// MARK: - Actions

// MARK: - Default functions

override func viewDidLoad() {
    super.viewDidLoad()
    tableCellArray = [projectDetails, location, electricityUse]
    configureCells()
    NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "refresh"), object: nil, queue: nil, using: refreshTable)
}

// MARK: - Table view data source

func configureCells() {
    var i = 0
    for each in tableCellArray {
        configureCheckmark(each, withIndex: i)
        i = i + 1
    }
}

func configureCheckmark(_ cell: UITableViewCell, withIndex index: Int) {
    var accessoryType: UITableViewCellAccessoryType = .none
    switch index {
        case 0 :
            accessoryType = .checkmark
        case 1 :
            if selectedProject?.completeLocation == true {
                accessoryType = .checkmark
            }
        case 2 :
            if selectedProject?.completeElectricity == true {
                accessoryType = .checkmark
            }
        default :
            accessoryType = .none
        }
    cell.accessoryType = accessoryType
}

func refreshTable(notification: Notification) -> Void {
    configureCells()
    tableView.beginUpdates()
    tableView.endUpdates()
}

这个给我修好了。希望有帮助:)


正确的解决方案!谢谢你的回答。在问题的末尾,我提到我尝试了DispatchQueue.main.async,但没有成功。我已经试着通过通知程序调用它,除非你建议另一个可能有效的位置。@ChrisBell:在这种情况下,我可能在核心问题上错了(尽管总是在主线程上放置UI更新)。您应该将代码更新为当前使用的任何代码,并确认正在调用refreshTable:。这将帮助我们缩小问题的范围。dylanthelion已经回答了关于.reloadData()和DispatchQueue.main.async的问题。至于BeginUpdate()和EndUpdate(),它们是不必要的。对于我自己和提问的人(阅读他的回答)。在我们的情况下,仅重新加载数据是不起作用的。只有在我添加BeginUpdate和EndUpdate时,它才能正常工作。在这种情况下,我想你应该解释一下为什么要这么做。这真的是可以帮助其他人的部分。:)(单击以执行此操作)我不知道为什么,但我还需要此操作序列来强制刷新tableView。在我的代码中仅仅调用reloadData是不够的,谢谢
 DispatchQueue.main.async {
            self.tableView.reloadData()
            self.tableView.beginUpdates()
            self.tableView.endUpdates()
        }