简体   繁体   中英

Call reloadData() to refresh TableView - Reusable Code

I need the tableView to perform reloadData() after a row has been added via a textView. My tableViews all use the reusable code which works fine. Below is my Reusable TableViewCode.

class ReusableSubtitleTable: NSObject, UITableViewDataSource, UITableViewDelegate{
        let cell = "cell"
        var dataArray = [String]()
        
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) {
            print("DataArray count from table view = \(dataArray.count)")
            return dataArray.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let selfSizingCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SelfSizingCell
            let num = indexPath.row
            selfSizingCell.titleText.text = (stepText[num])
            selfSizingCell.subtitleText.text = dataArray[num]
            return selfSizingCell
        }
    }

The function below uses the reusable code to display the table which works fine.

class DetailViewController: UIViewController {
     
    let step = 13
    var tableView: UITableView!
    let dataSource = ReusableSubtitleTable()
    var selectedEntry: JournalEntry!
    var dataModel = [String]()
    var didSave = false
    var coreDataManager = CoreDataManager()
     
     override func viewDidLoad() {
            super.viewDidLoad()
            tableView.delegate = dataSource
            tableView.dataSource = dataSource
            dataSource.dataArray = dataModel
     
    @IBAction func unwindToDetail( _ sender: UIStoryboardSegue) {
            dataModel[10] = step11
            didSave = coreDataManager.updateEntry(step11: step11, selectedEntry: selectedEntry)
}
}

The problem come in when a user wants to add to the last row. The user taps a button and is taken to the next controller which is a TextView. When user finishes their entry they tap the 'Save' button which returned to the DetailViewController via an unwind. The selectedEntry is saved and the dataModel updated. Now the table view needs to reload to display this added text.

I've tried adding tableView.ReloadData() after didSave. I've tried a Dispatch and tried saving the data before returning from the textView via the unwind but that doesn't work either.

I tried adding the below function to ReusableTableView and called it after the coredata update - there are no errors but it does not reload the table.

   func doReload(){
       tableView.reloadData()
    }

I have verified that the data is saved and it does displays if I return to the summary controller and then go forward the DetailViewController.

Any help is appreciated.

Placing the UITableView reloadData() within either viewWillAppear or viewDidAppear should resolve this issue.

For example:

func viewDidAppear(_ animated: bool) {
    super.viewDidAppear(animated)
    tableView.reloadData()
}

This is because the view hierarchy isn't yet regarded as "visible" during the segue unwind and why you see it work by going back to reloading the view controller. The reloadData() function, for efficiency, only redisplays visible cells and at the time of the unwind the cells aren't "visible".

Apple Documentation - UITableView.reloadData() :

For efficiency, the table view redisplays only those rows that are visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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