更新单击的表格视图单元格的问题

时间:2015-11-18 21:15:40

标签: ios uitableview

我有一个令我烦恼的问题,我觉得我很亲密。情况就是这样。我有一个包含不同微积分视频的tableview,我想让我的学生能够下载视频以便离线播放。那部分工作正常。我遇到的问题是,当用户点击下载按钮时,下载按钮应隐藏并显示活动指示符和进度条(所有这些都在自定义单元格中)。我有两个问题:

1)下载按钮不会自动隐藏并显示活动视图,除非我滚动它并返回它。

2)如果我向下滚动,随机单元格将不再有下载按钮。

这必定是因为细胞被重复使用,但我认为我做得很好,尽管显然有错误。我将在下面发布相关的代码片段:

cellForRowAtIndexPath代码:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("VideoCell", forIndexPath: indexPath) as! TableViewCell

        let currentVideo = videos[indexPath.section][indexPath.row]

        cell.downloadButton.addTarget(self, action: "downloadButtonPressed:", forControlEvents: .TouchUpInside)

        cell.video = currentVideo

        return cell
    }

自定义表格视图单元格代码:

    class TableViewCell: UITableViewCell {

        @IBOutlet weak var videoTitleLabel: UILabel!
        @IBOutlet weak var progressView: UIProgressView!
        @IBOutlet weak var customAccessoryView: UIView!
        @IBOutlet weak var downloadButton: UIButton!
        @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

        let userDefaults = NSUserDefaults.standardUserDefaults()

        var video: Video? {
            didSet {
                updateUI()
            }
        }

        private func updateUI() {
            guard let video = video else {
                print("video should not be nil")
                return
            }

            videoTitleLabel.text = video.title

            if video.downloadStatus.isDownloading {
                progressView.hidden = false
                progressView.progress = video.downloadStatus.downloadProgress
                downloadButton.hidden = true
                activityIndicator.hidden = false
                activityIndicator.startAnimating()
            } else {
                progressView.hidden = true
                activityIndicator.stopAnimating()
            }

            if video.downloadStatus.isSaved {
                activityIndicator.stopAnimating()
                progressView.hidden = true
                downloadButton.hidden = true
            }
        }

下载请求

func downloadButtonPressed(sender: UIButton) {
    let buttonPosition = sender.convertPoint(CGPointZero, toView: self.tableView)
    guard let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition) else {
        print("Error getting index path from button press")
        return
    }


    let section = indexPath.section
    let row = indexPath.row
    print("Button pressed at section \(section) and row \(row)")//correct row and section

    //Code to save Video to Documents directory goes here
    let currentVideo = videos[section][row]

    guard !currentVideo.downloadStatus.isSaved else {
        print("Video is already saved")
        return
    }

    guard let url = currentVideo.url else {
        print("Video not found...url is invalid")
        return
    }

    let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

    Alamofire.download(.GET, url, destination: destination)
        .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in

            let progress = Float(totalBytesRead) / Float(totalBytesExpectedToRead)
             currentVideo.downloadStatus.isDownloading = true
            currentVideo.downloadStatus.downloadProgress = progress

        }.response { request,response,data, error in
            if let error = error {
                print("Failed with error: \(error)")
            } else {
                print("Downloaded file successfully")
                currentVideo.downloadStatus.isDownloading = false
                currentVideo.downloadStatus.isSaved = true
                self.saveDownloadStatusInDefaultsForVideo(currentVideo, isSaved: true)
            }
            print("Files currently in the documents directory:")
            self.printDocumentsDirectoryContents() //file is there
    }
}

1 个答案:

答案 0 :(得分:0)

在您的班级TableViewCell中,您在某些情况下将hidden的媒体资源downloadButton设置为true

如果您希望按钮在默认情况下可见,则在重用单元格时将属性显式设置为false