簡體   English   中英

根據類似狀態更新 tableview 中使用的單元格中的按鈕,而無需重新加載單元格

[英]Update a button in a cell used in tableview depending on like status without reloading the cell

我正面臨一個奇怪的問題,即在單擊時更新單元格中的按鈕。 我有這個喜歡的按鈕,我改變它取決於我點擊按鈕時得到的響應狀態。 所以當喜歡狀態為假時。 如果后端的狀態發生變化,我將其顯示為灰色並單擊,如果我將狀態設為 true 作為響應,我會將其更改為粉紅色,反之亦然。 這個問題是第一次。 一旦我更改了單元格,功能就會按預期工作。

這是我的代碼

我為單元格創建了一個變量以全局訪問它

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)
}

一旦我收到后端的回復

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

在此處輸入圖片說明

你得到了錯誤的索引路徑。 嘗試以這種方式訪問​​單元格。

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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM