How to update tableview cell without reload data ?

13,593

Try using tableView.cellForRow to get the cell and configure it directly yourself only with the modified data:

// this should give you currently visible cell for indexPath
if let ratableCell = tableView.cellForRow(at: indexPath) as? RatableCell {
    // instead of telling tableView to reload this cell, just configure here
    // the changed data, e.g.:
    ratableCell.configureRate(10)
}
Share:
13,593
Admin
Author by

Admin

Updated on August 21, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a tableview cell. In this cell there is a view that plays video, and star rating under this video view.

    I want want user to not rate until watch video's %50 part. if user watched video's %50 part, user can rate this video.

    In my model class I set a timer. When user click a video play timer starting to play and if timer == video duration/2 user can rate. otherwise

    user show alert message..

    What I did : When timer == video duration/2 i reload the tableview / or just reload the row

    but video is stopped.

    How can I do this ? Any advice or sample code please ?

    Here is my play button custom delegate func

    if(vlist[index.row].timerIndex >= 10){
                            print("====================!!!!!!!!!!^^^^^^^^^^^^^^======RATABLEEEEEEEEEEE")
                            vlist[index.row].isRatable = true
                            self.rateUserTableView.reloadRows(at: [index], with: .none)
                            MediaManager.sharedInstance.player?.embeddedContentView = cell.videoPlayerView
                            vlist[index.row].timer?.invalidate()
                        }
    

    and here is my tableview ccellForRowAt

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            guard let vlist = videoList ,!vlist.isEmpty else {let cell = UITableViewCell()
                cell.selectionStyle = .none
                cell.backgroundColor = .clear
                cell.textLabel?.textAlignment = .center
                cell.textLabel?.textColor = UIColor.black
                cell.textLabel?.text = "nodataavaiable".localized()
                return cell
            }
            let cell = tableView.dequeueReusableCell(withIdentifier: "RateUserCell", for: indexPath) as! RateUserCell
            //---
            let playing = vlist[indexPath.row].isPlaying
            cell.delegate = self
            cell.indexPath = indexPath
            cell.playButton.isHidden = playing
            cell.titleView.isHidden = playing
            //---
            if(indexPath.row % 2 == 0) {
                cell.thumbnailImageView.image = UIImage(named:"thumb1")
            }
            else {
                cell.thumbnailImageView.image = UIImage(named:"thumb2")
            }
            cell.titleLabel.text = vlist[indexPath.row].shortDesc
            cell.userName.text = vlist[indexPath.row].person
            cell.userCount.text = String(vlist[indexPath.row].voteCount!)
            cell.voteCount.text = String(vlist[indexPath.row].sumScore!)
            cell.cosmos.rating = Double(vlist[indexPath.row].userVote!)
    
    
    
            if(self.statusId == 3 ){
                if(vlist[indexPath.row].userVote! > 0){
                    cell.cosmos.didFinishTouchingCosmos =  { rating in
                        self.setVote(videoId: vlist[indexPath.row].videoId!, vote: Int(rating))
                    }
                }
                else {
    
                    if(vlist[indexPath.row].isRatable == true){
                        cell.cosmos.didTouchCosmos = { rating in
                            cell.cosmos.settings.updateOnTouch = true
                            cell.cosmos.rating = rating
                            self.setVote(videoId: vlist[indexPath.row].videoId!, vote: Int(rating))
                        }
                    }
                    else {
                        cell.cosmos.didTouchCosmos = { rating in
                            cell.cosmos.rating = Double(0.0)
                            self.showAlertMessage(vc: self, titleStr: "error".localized(), messageStr: "rateTimeMsj".localized(), img: "ntf-5", buttonColor: UIColor.red)
                            cell.cosmos.settings.updateOnTouch = false
                        }
    
                    }
    
                }
    
            }
            else if(self.statusId == 4){
                cell.cosmos.isHidden = true
            }
            return cell
        }