0

I am facing this weird problem of updating a button in a cell on click. I have this like button which i change depend on status i get in response when i click the button. So when the like status is false. I show it in grey and on click if the status in back-end changes and if i get the status as true in response i change it to pink and vice- versa. The issue is for the first time. once i change the cell the functionality works as expected.

Here is my code

I made a variable for the cell to access it globally

var TheVideoPlayerCell:VideoPlayerCell?


func registerTableCells(){
    myTable.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
    myTable.register(UINib(nibName: "VideoPlayerCell", bundle: nil), forCellReuseIdentifier: "VideoPlayerCell")
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return videoArrObj?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "VideoPlayerCell", for: indexPath) as! VideoPlayerCell
    TheVideoPlayerCell = cell
    cell.obj = videoArrObj?[indexPath.row]
    cell.btn_Likes.addTarget(self, action: #selector(onClickLike), for: .touchUpInside)
    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return CGFloat(self.myTable.frame.height)
}

Once i get a response from backend

self.TheVideoPlayerCell?.btn_Likes.isEnabled = true
let obj = try JSONDecoder().decode(LikeStatusModal.self, from: data)
let likeStatus = obj.data?.like_status ??  false
let totalLikes = obj.data?.total_likes ?? ""
if likeStatus{
    self.TheVideoPlayerCell?.btn_Likes.setImage(UIImage(named: "icon_like_selected"), for: .normal)
 }else{
    self.TheVideoPlayerCell?.btn_Likes.setImage(UIImage(named: "icon_like_unselected"), for: .normal)
 }
 if totalLikes != ""{
      self.TheVideoPlayerCell?.lbl_NoOfLikes.text = totalLikes
 }

 self.TheVideoPlayerCell?.obj?.is_like = likeStatus
 self.TheVideoPlayerCell?.obj?.likes = totalLikes
 self.videoArrObj?[self.currentIndex].is_like = likeStatus
 self.videoArrObj?[self.currentIndex].likes = totalLikes

enter image description here

Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25
  • ***I made a variable for the cell to access it globally*** - Delete that. And refer to this link: https://stackoverflow.com/questions/39947076/uitableviewcell-buttons-with-action – Keshu R. Jan 11 '20 at 05:30
  • sure.. will look at it.. Thanks – Yogesh Tandel Jan 11 '20 at 05:33
  • Hi.. the problem for me is not getting the index.. I get the correct index on click.. The problem is updating the button once i have the response for that cell. I do not want to reload the cell. – Yogesh Tandel Jan 11 '20 at 06:16
  • **The issue is for the first time.** can you elaborate? You mean for the first row the like button dont work? Right? – Keshu R. Jan 11 '20 at 06:36
  • Yes.. the cell has the full height and width of the frame. The click update the object of the array for that index after i get response. The issue is for the first row only. once i go to second row, everything works perfect. Even when i scroll back to first row every thing works perfect. – Yogesh Tandel Jan 11 '20 at 06:52
  • updated the answer. – Keshu R. Jan 11 '20 at 07:18
  • Bro.. I managed to resolve the issue but not with the below solution. I made use of cellDelegate as you had given in the link. I will update with my answer once i finish. – Yogesh Tandel Jan 11 '20 at 07:21

1 Answers1

0

You are getting the wrong indexpath. Try accessing the cell this way.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "VideoPlayerCell", for: indexPath) as! VideoPlayerCell

    cell.obj = videoArrObj?[indexPath.row]
    cell.btn_likes.tag = indexPath.row
    cell.btn_Likes.addTarget(self, action: #selector(onClickLike(sender:)), for: .touchUpInside)
    cell.selectionStyle = .none
    return cell
}


func onClickLike(sender : UIButton) {
    // Here is have the api request for that video obj
    ServiceSuccessData(action : "YourString", data : Data, index : sender.tag)
}
//Once I get response from server
func ServiceSuccessData(action:String, data:Data, index : Int){
    let cell = tableView.cellForRow(at: IndexPath(row: index, section: 0)) as! YourCell
    cell.btn_Likes.isEnabled = true

    DispatchQueue.main.async {
        let obj = try JSONDecoder().decode(LikeStatusModal.self, from: data)
    let likeStatus = obj.data?.like_status ??  false
    let totalLikes = obj.data?.total_likes ?? ""
    if likeStatus{
        cell.btn_Likes.setImage(UIImage(named: "icon_like_selected"), for: .normal)
     } else {
        cell.btn_Likes.setImage(UIImage(named: "icon_like_unselected"), for: .normal)
     }
     if totalLikes != ""{
          cell.lbl_NoOfLikes.text = totalLikes
     }
     cell.obj?.is_like = likeStatus
     cell.obj?.likes = totalLikes
     self.videoArrObj?[self.currentIndex].is_like = likeStatus
     self.videoArrObj?[self.currentIndex].likes = totalLikes
    }
}
Keshu R.
  • 5,045
  • 1
  • 18
  • 38