Apocal
Apocal

Reputation: 172

TableView reload from Segue not working in Swift 3

In Swift v3, my TableView is not reloading upon segue. I am pretty sure that this code was working in 2.0.

@IBAction func unwindToLevelListView(_ segue: UIStoryboardSegue)
{
    print("RELOADING VIEW")
    TableView.reloadData()
}

RELOADING VIEW prints, but the TableView does not reload. I've also tried putting this code in ViewDidLoad and ViewDidAppear, but neither of those worked.

P.S. A point of note, the rows also don't reload upon display (i.e. scroll down and then back up).

Assistance is greatly appreciated.

Code Follows:

override func viewDidAppear(_ animated: Bool)
{
    TableView.reloadData()
}

// Create Table
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "LevelSelected", for: indexPath) as! LevelListCell

    // Set labels
    cell.levelNameLabel.text = "LEVEL " + String(puzzleLevels[(indexPath as NSIndexPath).row])
    let unlocked: Bool = puzzleUnlocked[(indexPath as NSIndexPath).row]

    if(challengeSelected == 1 && (indexPath as NSIndexPath).row == 0 && puzzleUnlocked[1] == false)
    {
        cell.levelStatusLabel.text = "Not Solved"
    }
    else if(unlocked == true)
    {
        cell.levelStatusLabel.text = "Solved"
    }
    else
    {
        cell.levelStatusLabel.text = "Not Solved"
    }

    // Set color
    cell.levelNameLabel.textColor = UIColor.white
    cell.levelStatusLabel.textColor = UIColor.white

    if unlocked == false
    {
        cell.backgroundColor = UIColor.lightGray
        // Uncomment to disable interaction
        // cell.userInteractionEnabled = false // Comment Out to Remove Lock
    }
    else if unlocked == true
    {
        cell.backgroundColor = UIColor(red: 0, green: 64, blue: 128, alpha: 0.0)
        cell.isUserInteractionEnabled = true
    }

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return puzzleLevels.count
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    levelSelected = (indexPath as NSIndexPath).row

    self.performSegue(withIdentifier: "LevelSelected", sender: levelSelected);
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if (segue.identifier == "LevelSelected")
    {
        if let nextViewController = (segue.destination as? GameViewController)
        {
            nextViewController.selectedLevel = levelSelected
            nextViewController.selectedChallenge = challengeSelected
        }
        print("List View Level Selected:\(levelSelected)")
    }
}

@IBAction func unwindToLevelListView(_ segue: UIStoryboardSegue)
{
    print("RELOADING VIEW")
    TableView.reloadData()
}

Upvotes: 1

Views: 717

Answers (2)

Apocal
Apocal

Reputation: 172

Thank you for the responses. I was able to figure out what is going on. The most important thing when doing the segue is to ensure that values are re-established.

@IBAction func unwindToLevelListView(_ segue: UIStoryboardSegue)
{
    print("RELOADING VIEW")
    let getLevels = Level(challengeNumber: challengeSelected, gameSelected: gameSelected)
    puzzleLevels = getLevels.puzzleLevels
    puzzleUnlocked = getLevels.unlocked
    tableView.reloadData()
}

Upvotes: 0

Frederik Frandsen
Frederik Frandsen

Reputation: 15

NotificationCenter.default.addObserver(self, selector:"reloadData", name:
NSNotification.Name(rawValue: "reloadTableView"), object: nil)

func reloadData(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            print("Work SO !!")
            self.tableView.reloadData()
        }
}

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTableView"), object: nil)

Upvotes: 1

Related Questions