Okay I will show the complete code. The error I am getting is the name being transferred is nil. The willSelectRowAt is does not extend the scope of the selectedNameInCell.
class FavouritesVC: UITableViewController {
var currentFav: CurrentPlayers?
var selectedNameInCell : String?
var favSet: OrderedSet<CurrentPlayers> {
FavouriteManager.shared.favSet
}
override func viewDidLoad() {
super.viewDidLoad()
if currentFav == nil {
//display nil
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
} else {
NotificationCenter.default.post(name: .passFavNotification,
object: self.currentFav)
}
}
@objc
func handleFavNotification(notification: Notification) {
tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favSet.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 152
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "favcell", for: indexPath) as! FavCell
let itemFav = favSet[favSet.index(favSet.startIndex, offsetBy: indexPath.row)]
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 0.5
cell.contentView.backgroundColor = .random
cell.update(with: itemFav)
return cell
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let cell = tableView.cellForRow(at: indexPath) as? FavCell {
selectedNameInCell = cell.name.text
}
return indexPath
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "hs" {
print("this is the segue destination: \(segue.destination)")
print("name being transferred is \(selectedNameInCell)")
if let destinationController = segue.destination as? HighlightsVC, let destName = selectedNameInCell {
destinationController.playerName = destName
}
}
}
}