从 Segue 重新加载 TableView 在 Swift 3 中不起作用
TableView reload from Segue not working in Swift 3
在 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()
}
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)
感谢您的回复。我能够弄清楚发生了什么。进行 segue 时最重要的是确保重新建立值。
@IBAction func unwindToLevelListView(_ segue: UIStoryboardSegue)
{
print("RELOADING VIEW")
let getLevels = Level(challengeNumber: challengeSelected, gameSelected: gameSelected)
puzzleLevels = getLevels.puzzleLevels
puzzleUnlocked = getLevels.unlocked
tableView.reloadData()
}
在 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()
}
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)
感谢您的回复。我能够弄清楚发生了什么。进行 segue 时最重要的是确保重新建立值。
@IBAction func unwindToLevelListView(_ segue: UIStoryboardSegue)
{
print("RELOADING VIEW")
let getLevels = Level(challengeNumber: challengeSelected, gameSelected: gameSelected)
puzzleLevels = getLevels.puzzleLevels
puzzleUnlocked = getLevels.unlocked
tableView.reloadData()
}