Reputation:
i have a tableview that is not removing data from the cell after i remove all items from array and call tableview.reloadata, in the tableview cell i have a single label and the text stays
gameScore is an instance of my class, that has a func to remove all items from array
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()
}
}))
i call the func using the delegate method on another viewcontroller
if teamA_totalScore >= winningScore {
print("Winner")
self.dismiss(animated: true, completion: nil)
delegate?.reset(winnerPlayer: (delegate?.gameScore.teamA_name)!)
}
}else{
delegate?.teamA_totalScore.text = ""
}
this is my popUp view controller where declare the 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?
....
}
this is my gameScore view controller and where i set the delegate
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPopUp"{
let popUpView = segue.destination as! PopUpViewController
popUpView.delegate = self
}
i"m calling the reset func that is in my gameScore view controller from my popUp view controller
this is my tableview delegate and datasource, i'm only using teamA_tableView to test the func reset, i did tried using both and teamB_tableview but didn't work aswell
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()
}
}
Upvotes: 1
Views: 546
Reputation: 179
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()
}
I don't know how it works before reload data, by the look of your cellForRowAtIndexpath, you are returning a new UITableViewCell() instance but not your ScoreTableViewCell or ScoreBTableViewCell
should look like this:
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()
}
}
Additionally a recommendation: Both for A team's score and B team's score you should probably have the same cell and not two different kind of cells something like ScoreTableviewCell. You just change the team name, jersey, color, etc.
Upvotes: 3