Okay I have another error with deleting: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Post
Replies
Boosts
Views
Activity
Okay I guess I can turn it into an array it would make it so much simpler. I really don't understand how to set it.
Okay I can give you the favcell code it's a custom cell, the segue to HighlightsVC is put where the button is.
class FavCell: UITableViewCell {
let imageController = FetchImage()
@IBOutlet weak var favImg: UIImageView!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var team: UILabel!
@IBOutlet weak var position: UILabel!
@IBOutlet weak var highlights: UIButton!
@IBOutlet weak var stats: UIButton!
func update(with itemFav: CurrentPlayers) {
name.text = itemFav.yahooName
team.text = itemFav.team
position.text = itemFav.position
favImg.image = UIImage(named: "Grey")
let url = URL(string: itemFav.photoUrl!)
imageController.fetchItemArtwork(url: url!) {
(image) in
if let image = image {
DispatchQueue.main.async {
self.favImg.image = image
self.favImg.roundedImage()
}
}
}
}
func goToSite() {
if let url = URL(string: "https://www.hockey-reference.com/") {
UIApplication.shared.open(url, options: [:])
}
}
}
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
}
}
}
}
I analyzed it and there's 2 segues that are going to the VC. So that's why it's being called twice. I have to get rid of the button action and do a segue with storyboard.
Okay I will give you more detail I have a function that retrieves youtube data but it is called twice.
func fetchYoutubeData(completion: @escaping () -> ()) {
let API_Key = "key"
let theName = playerName
print("this is the player name that was transferred \(playerName)")
let query: [String: String] = ["part" : "snippet", "maxResults" : "5", "q" : theName + "nhl player highlights", "type" : "video", "key" : API_Key]
let baseURL = URL(string: "https://www.googleapis.com/youtube/v3/search")!
let url = baseURL.withQueries(query)!
print(url)
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
DispatchQueue.main.async {
do {
let youtube = try JSONDecoder().decode(Highlights.self, from: data)
self.youtubeArr = youtube.items
self.tableView.reloadData()
} catch let jsonErr {
print("Error serializing json:", jsonErr)
}
}
}.resume()
}
By twice I mean the VC that I go to calls the API twice.
override func viewDidLoad() {
super.viewDidLoad()
fetchYoutubeData {
self.tableView.reloadData()
}
}
Okay I have it working but it goes to the vc twice and it also does not transfer the name.
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let cell = tableView.cellForRow(at: indexPath) as? FavCell {
selectedNameInCell = cell.name.text
print("destName is \(String(describing: selectedNameInCell))") //does not print name
}
return indexPath
}
The error comes from my camButtonA. Unrecognized selector
@IBAction func CamButtonA(_ sender: UIButton) {
performSegue(withIdentifier: "hs", sender: nil)
}
Okay I get the same error unrecognized selector sent to instance '0x7fd4c4098200'. I tried both ways but end up with the same error. You don't but the button in the cell?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "hs" {
if let destinationController = segue.destination as? HighlightsVC, let destName = selectedNameInCell {
destinationController.playerName = destName
}
}
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let cell = tableView.cellForRow(at: indexPath) as? FavCell {
selectedNameInCell = cell.name.text
}
return indexPath
}
@IBAction func CamButtonA(_ sender: UIButton) {
performSegue(withIdentifier: "hs", sender: nil)
}
I have a custom cell actually. I have the function I used to display it at the bottom. I am curious with willRowAt do I return indexPath?
func update(with itemFav: CurrentPlayers) {
name.text = itemFav.yahooName
team.text = itemFav.team
position.text = itemFav.position
favImg.image = UIImage(named: "Grey")
let url = URL(string: itemFav.photoUrl!)
imageController.fetchItemArtwork(url: url!) {
(image) in
if let image = image {
DispatchQueue.main.async {
self.favImg.image = image
self.favImg.roundedImage()
}
}
}
}
okay thanks but I get the error Value of type 'UITableViewCell' has no member 'yahooName'. I should show my cellForRowAt. I can also access yahooName with currentFav.yahooName but not sure how to access from the cell.
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
}
Okay I have a new error this time. Cannot convert value of type 'IndexPath.Type' to expected argument type 'IndexPath'
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let cell = tableView.cellForRow(at: IndexPath) {
selectedNameInCell = cell.yahooName
}
}
Okay so I downloaded a swift package that gives me access to an orderedSet so I can solve that issue but thanks for your suggestion. Anyways I get the cannot find cellForRow error in scope.
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let cell = cellForRow(at: IndexPath) {
selectedNameInCell = cell.yahooName
}
}
Okay I will do that. I have a set because I wanted to avoid duplicates with an array.