ios - 如何在不重新加载数据的情况下更新 tableview 单元格?

标签 ios swift uitableview swift4

我有一个表格 View 单元格。 在这个单元格中有一个播放视频的 View ,在这个下面有星级 视频观看。

我希望用户在观看视频的 %50 部分之前不要评分。 如果用户观看了视频的 %50 部分,用户可以对该视频进行评分。

在我的模型类中,我设置了一个计时器。当用户点击视频播放计时器开始播放时,如果计时器 == 视频持续时间/2 用户可以评分。否则

用户显示警告消息..

我做了什么:当计时器 == 视频时长/2 我重新加载 tableview/或者只是重新加载行

但是视频停止了。

我该怎么做?请提供任何建议或示例代码?

这是我的播放按钮自定义委托(delegate)函数

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

这是我的 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
    }

最佳答案

尝试使用 tableView.cellForRow 获取单元格并仅使用修改后的数据自己直接配置它:

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

关于ios - 如何在不重新加载数据的情况下更新 tableview 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901949/

相关文章:

ios - 尝试读取已解析的 JSON 数组时索引超出范围

swift - 使用 firebase 填充 tableview 时,视频源的每个视频都与最近上传的视频相同

ios - GMSMarker 不透明度动画不重复

iphone - 如果我的构建目标是 iOS 5.0,当较旧的操作系统版本尝试下载/运行它时会发生什么情况?

ios - 数组在错误的位置返回值

xcode - Swift 中的字符串问题

ios - 渐变背景iOS

ios - 有没有办法强制更改您的 iOS 推送通知 deviceToken?

ios - 必须提供 secret 或公钥 - IOS/Swift + Socket.io-JWT

ios - 如何在 UITableView 上只圆角?