【问题标题】:How to update tableview cell without reload data ?如何在不重新加载数据的情况下更新 tableview 单元格?
【发布时间】:2018-07-31 19:12:28
【问题描述】:

我有一个表格视图单元格。 在此单元格中,有一个播放视频的视图,以及此下方的星级评分 视频观看。

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

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

用户显示警报消息..

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

但视频已停止。

我该怎么做?请问有什么建议或示例代码吗?

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

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

这是我的表格视图 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
    }

【问题讨论】:

  • 不相关,但为什么videoList 是可选的,为什么每次调用cellForRow 时都要检查空数组(这种情况经常发生)。这是不必要的昂贵。数据源数组应该是非可选的,如果 numberOfRows 返回 0,则根本不会调用 cellForRow。从数组 (vlist[indexPath.row]) 中获取相同的对象 6 或 7 次是不必要的昂贵,也是。
  • 为什么需要重新加载表格或单元格??
  • 因为在阅读了您的问题后,我明白您希望视频继续播放。

标签: ios swift uitableview swift4


【解决方案1】:

您有两种选择。 您可以重新加载特定的单元格,然后重新加载表格视图。

使用:-

tableView.reloadRows(at: [indexPath], with: .top)

或者您可以获得特定单元格的实例,您可以根据您的要求对其进行修改。

使用:-

guard let particularCell = tableView.cellForRows(at: indexPath) as? CellName else {
return 
}

【讨论】:

    【解决方案2】:

    尝试使用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)
    }
    

    【讨论】:

      【解决方案3】:

      尝试使用它的标识符声明您的单元格并使用 awakFromnib(),为我工作

       if let receiverCell = self.tableView.dequeueReusableCell(withIdentifier: "Receiver", for: indexe) as? ReceiverCell {
      
      //    instead of telling tableView to reload this cell, just configure here
      receiverCell.awakeFromNib()
      
       }
      

      【讨论】:

        【解决方案4】:

        尝试使用此代码在特定索引路径上重新加载单行/多行。

        tableView.reloadRows(at: [indexPath!], with: .top)
        

        【讨论】:

          【解决方案5】:

          tableView.numberOfItems(inSection: 0) 这一行不会做任何事情,但它会在没有reloadData 的情况下刷新您的数据。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-11-01
            • 1970-01-01
            • 2014-05-13
            • 2015-01-06
            • 2021-01-10
            • 1970-01-01
            • 2015-10-09
            相关资源
            最近更新 更多