简体   繁体   English

Swift 3 tableView 未在 reloadData() 上更新

[英]Swift 3 tableView not updating on reloadData()

I am building a master detail app.我正在构建一个主细节应用程序。 I have a sections view controller (VC) which is a child of master VC.我有一个截面视图 controller (VC),它是主 VC 的子级。 Clicking on rows in the sections VC brings up different detail VCs where users complete required information.单击 VC 部分中的行会显示不同的详细 VC,用户可以在其中填写所需信息。 Once the information in a detail VC is completed I would like to have a checkmark display in the relevant section VC row.完成详细 VC 中的信息后,我希望在相关部分 VC 行中显示复选标记。 FYI selectedProject is a core data object. FYI selectedProject 是一个核心数据 object。

I call a notification in the detail VC viewWillDisappear to refresh the sections VC.我在详细 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()
}

Console:安慰:

Notification ran通知已运行

location complete is true位置完成是真的

electricity complete is true电齐全是真的

Project Details项目详情

3 3

location complete is true位置完成是真的

electricity complete is true电齐全是真的

Location地点

3 3

location complete is true位置完成是真的

electricity complete is true电齐全是真的

Electricity Use用电量

3 3

The 3's =.checkmark 3 的 =.checkmark

So it appears cellForRowAt knows exactly what it should be doing but then the table is not readily displaying the checkmarks.所以看起来 cellForRowAt 确切地知道它应该做什么,但是表格并不容易显示复选标记。 Instead you either have to click back to the master VC then forward to the sections VC to get the checkmarks displaying, or you have to click different rows in the section VC and the checkmarks will randomly show up.相反,您必须单击返回到主 VC,然后转发到 VC 部分以显示复选标记,或者您必须单击 VC 部分中的不同行,复选标记将随机显示。

I've tried calling reloadData in dispatchqueue.main.async with no luck.我试过在 dispatchqueue.main.async 中调用 reloadData,但没有成功。 I tried calling reloadData at different points in the process again without success.我尝试在过程中的不同点再次调用 reloadData ,但没有成功。 I've tried calling tableView.beginUpdates/.endUpdates and reloadSectionData, again without success to get the checkmarks to display.我已经尝试调用 tableView.beginUpdates/.endUpdates 和 reloadSectionData,但仍然没有成功显示复选标记。

If I programmatically prompt section VC viewWilDisappear and viewWillAppear in succession, the checkmarks will appear once a different row in sections VC is selected.如果我以编程方式连续提示部分 VC viewWilDisappear 和 viewWillAppear,则一旦选择部分 VC 中的不同行,就会出现复选标记。 it "seems" like reloadData() isn't prompting a complete reload of the tableView.它“似乎”像 reloadData() 并没有提示完全重新加载 tableView。

FYI accessory item is set to.none in the storyboard.仅供参考,附件项目在 storyboard 中设置为 .none。

UI updates need to be called on the main queue. 需要在主队列上调用UI更新。 Try this: 尝试这个:

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

Ok so in light of no fixes forthcoming, I went and changed the tableView to static rather than dynamic. 好吧,鉴于没有即将修复,我去了将tableView更改为静态而不是动态。 I created tableViewCell outlets for all the fixed cells and then added and removed the checkmarks that way. 我为所有固定单元创建了tableViewCell出口,然后以这种方式添加和删除了复选标记。

The code for anyone interested: 任何感兴趣的人的代码:

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()
}

This fixed it for me. 这为我修好了。 Hope it helps :) 希望能帮助到你 :)

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

Make sure you have used these two lines in viewDidLoad():确保你在 viewDidLoad() 中使用了这两行:

//registering the xib
self.tableView.register(UINib(nibName: "enter_xib_class_name", bundle: nil), forCellReuseIdentifier: "enter_cell_identifier")
//delegate enabling
self.tableView.delegate=self
//datasource
self.tableView.datasource=self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM