在Swift v3中,我的TableView没有在segue上重新加载。我很确定这段代码在2.0版本中工作。
@IBAction func unwindToLevelListView(_ segue: UIStoryboardSegue)
{
print("RELOADING VIEW")
TableView.reloadData()
}
RELOADING VIEW打印,但TableView不会重新加载。我也尝试将此代码放在ViewDidLoad和ViewDidAppear中,但这些都没有。
P.S。值得注意的是,行也不会在显示时重新加载(即向下滚动然后向上滚动)。
非常感谢协助。
代码遵循:
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()
}
答案 0 :(得分:1)
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)
答案 1 :(得分:0)
感谢您的回复。我能够弄清楚发生了什么。执行segue时最重要的事情是确保重新建立值。
@IBAction func unwindToLevelListView(_ segue: UIStoryboardSegue)
{
print("RELOADING VIEW")
let getLevels = Level(challengeNumber: challengeSelected, gameSelected: gameSelected)
puzzleLevels = getLevels.puzzleLevels
puzzleUnlocked = getLevels.unlocked
tableView.reloadData()
}