【问题标题】:Swift 3 tableView not updating on reloadData()Swift 3 tableView 未在 reloadData() 上更新
【发布时间】:2017-03-04 05:12:32
【问题描述】:

我正在构建一个主从应用程序。我有一个部分视图控制器(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 的 = .勾号

所以看起来 cellForRowAt 确切地知道它应该做什么,但是表格并不容易显示复选标记。相反,您要么必须单击返回到主 VC,然后转发到 VC 部分以显示复选标记,或者您必须单击 VC 部分中的不同行,复选标记将随机显示。

我尝试在 dispatchqueue.main.async 中调用 reloadData,但没有成功。我尝试在过程中的不同点再次调用 reloadData ,但没有成功。我已经尝试调用 tableView.beginUpdates/.endUpdates 和 reloadSectionData,但仍然没有成功显示复选标记。

如果我以编程方式连续提示部分 VC viewWilDisappear 和 viewWillAppear,则在选择部分 VC 中的不同行后,复选标记将出现。它“似乎”像 reloadData() 没有提示完全重新加载 tableView。

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

【问题讨论】:

    标签: ios swift xcode uitableview checkmark


    【解决方案1】:

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

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

    【讨论】:

    • 正确的解决方案!
    • 感谢您的回答。在我提到的问题的底部,我尝试过 DispatchQueue.main.async 但没有成功。我已经尝试通过通知程序调用它,除非您建议另一个可能有效的位置。
    • @ChrisBell:在这种情况下,我可能对核心问题有误(尽管 DO 总是将 UI 更新放在主线程上)。您应该将代码更新为您当前使用的任何内容,并确认正在调用 refreshTable:。这将帮助我们缩小问题范围。
    【解决方案2】:

    这为我解决了问题。希望对你有帮助:)

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

    【讨论】:

    • dylanthelion 已经回答了关于 .reloadData() 和 DispatchQueue.main.async 的问题。至于 beginUpdates() 和 endUpdates(),它们不是必需的。
    • 对于我自己和提出问题的人(阅读他的回复),仅 .reloadData 在我们的情况下不起作用。只有当我添加 beginUpdates 和 endUpdates 时它才能正常工作。
    • 好吧,在这种情况下,我认为您应该解释为什么必须这样做。真的,这将是可以帮助其他人的部分。 :) (点击edit 这样做)
    • 我不知道为什么,但我也需要这个操作序列来强制刷新 tableView。仅在我的代码上调用 reloadData 是不够的谢谢
    【解决方案3】:

    好吧,鉴于没有修复即将到来,我将 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()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多