3

我正在构建一个主细节应用程序。我有一个部分视图控制器(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。

4

3 回答 3

14

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

DispatchQueue.main.async {
    self.tableView.reloadData()
}
于 2017-03-04T05:20:24.803 回答
1

这为我修好了。希望能帮助到你 :)

 DispatchQueue.main.async {
            self.tableView.reloadData()
            self.tableView.beginUpdates()
            self.tableView.endUpdates()
        }
于 2018-11-22T21:43:30.440 回答
0

好的,鉴于没有即将到来的修复,我将 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()
}
于 2017-03-08T03:21:25.713 回答