在我从数组中删除所有项目并调用 tableview.reloadata 后,我有一个 tableview 没有从单元格中删除数据,在 tableview 单元格中我有一个标签并且文本保留
gameScore 是我的类的一个实例,它具有从数组中删除所有项目的功能
func reset(winnerPlayer: String){
let alertView = UIAlertController(title: "Winner:" + " " + "\(winnerPlayer)", message: "", preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "Restart", style: .destructive, handler: { action in
print("Tapped")
self.gameScore.resetArrays()
DispatchQueue.main.async {
self.teamA_tableview.reloadData()
}
}))
我在另一个 View Controller 上使用委托(delegate)方法调用函数
if teamA_totalScore >= winningScore {
print("Winner")
self.dismiss(animated: true, completion: nil)
delegate?.reset(winnerPlayer: (delegate?.gameScore.teamA_name)!)
}
}else{
delegate?.teamA_totalScore.text = ""
}
这是我的弹出 View Controller ,其中声明了委托(delegate)
class PopUpViewController: UIViewController {
@IBOutlet weak var whiteView: UIView!
@IBOutlet weak var teamsNameSegControl: UISegmentedControl!
@IBOutlet weak var pointsTextField: UITextField!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var addButton: UIButton!
var winningScore = Int()
weak var delegate: GameScoreViewController?
....
}
这是我的 gameScore View Controller ,我在哪里设置委托(delegate)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPopUp"{
let popUpView = segue.destination as! PopUpViewController
popUpView.delegate = self
}
我正在从我的弹出 View Controller 调用我的 gameScore View Controller 中的重置函数
这是我的 tableview 委托(delegate)和数据源,我只使用 teamA_tableView 来测试 func 重置,我确实尝试过同时使用和 teamB_tableview 但也没有用
extension GameScoreViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var numberOfRows = Int()
if tableView == teamA_tableview{
numberOfRows = gameScore.teamA_points.count
}else if tableView == teamB_tableview{
numberOfRows = gameScore.teamB_points.count
}
return numberOfRows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == teamA_tableview {
let cell = tableView.dequeueReusableCell(withIdentifier: "teamA_scoreCell", for: indexPath) as! ScoreTableViewCell
cell.teamA_label.text = String(gameScore.teamA_points[indexPath.row])
}else if tableView == teamB_tableview {
let cell = tableView.dequeueReusableCell(withIdentifier: "teamB_scoreCell", for: indexPath) as! ScoreBTableViewCell
cell.teamB_label.text = String(gameScore.teamB_points[indexPath.row])
}
return UITableViewCell()
}
}
最佳答案
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == teamA_tableview {
let cell = tableView.dequeueReusableCell(withIdentifier: "teamA_scoreCell", for: indexPath) as! ScoreTableViewCell
cell.teamA_label.text = String(gameScore.teamA_points[indexPath.row])
} else if tableView == teamB_tableview {
let cell = tableView.dequeueReusableCell(withIdentifier: "teamB_scoreCell", for: indexPath) as! ScoreBTableViewCell
cell.teamB_label.text = String(gameScore.teamB_points[indexPath.row])
}
return UITableViewCell()
}
我不知道在重新加载数据之前它是如何工作的,从你的 cellForRowAtIndexpath 来看,你正在返回一个新的 UITableViewCell() 实例,但不是你的 ScoreTableViewCell 或 ScoreBTableViewCell
应该是这样的:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == teamA_tableview {
let cell = tableView.dequeueReusableCell(withIdentifier: "teamA_scoreCell", for: indexPath) as! ScoreTableViewCell
cell.teamA_label.text = String(gameScore.teamA_points[indexPath.row])
return cell
} else if tableView == teamB_tableview {
let cell = tableView.dequeueReusableCell(withIdentifier: "teamB_scoreCell", for: indexPath) as! ScoreBTableViewCell
cell.teamB_label.text = String(gameScore.teamB_points[indexPath.row])
return cell
} else {
return UITableViewCell()
}
}
另外一个建议:对于 A 团队的分数和 B 团队的分数,您可能应该有相同的单元格,而不是像 ScoreTableviewCell 这样的两种不同类型的单元格。您只需更改球队名称、 Jersey 、颜色等。
关于ios - 调用 array.removeAll() 后 tableview.reload 不起作用 swift 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385321/