2017-03-04 251 views
3

我正在構建一個主細節應用程序。我有一個章節視圖控制器(VC),這是VC大師的孩子。單擊VC部分中的行可以提供不同的細節VC,用戶可以在其中完成所需的信息。一旦詳細信息VC中的信息完成,我想在相關部分VC行中有複選標記顯示。 FYI selectedProject是核心數據對象。swift 3 tableView在reloadData上沒有更新()

我在詳細的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的= .checkmark

所以它出現cellForRowAt確切地知道它應該做的事情,但隨後該表不容易顯示選中標記。相反,您必須單擊回主VC,然後轉到VC部分才能顯示覆選標記,或者必須單擊VC部分中的不同行,並且複選標記將隨機顯示。

我已經嘗試在dispatchqueue.main.async中調用reloadData,但沒有運氣。我試着在這個過程的不同點再次調用reloadData,但沒有成功。我試着調用tableView.beginUpdates/.endUpdates和reloadSectionData,再次沒有成功獲得顯示的複選標記。

如果我以編程方式提示部分VC viewWilDisappear和viewWillAppear連續,選中部分VC中的不同行後,將出現複選標記。它「似乎」像reloadData()不提示完整的重新加載tableView。

FYI配件項目在故事板中設置爲.none。

回答

7

需要在主隊列上調用UI更新。試試這個:

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

正確的解決方案! – Mannopson

+0

感謝您的回答。對我提到的問題的底部,我提到我已經嘗試DispatchQueue.main.async沒有成功。我試着通過通知器來調用它,除非你會建議另一個可能工作的位置。 –

+0

@ChrisBell:在這種情況下,我可能在覈心問題上是錯誤的(儘管DO總是在主線程上放置UI更新)。你應該更新你的代碼到你正在使用的任何東西,並確認正在調用refreshTable:。這將有助於我們縮小問題的範圍。 – dylanthelion

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() 
} 
相關問題