Video TableViewCell Cause Memory Error in Swift

断了今生、忘了曾经 提交于 2019-12-11 10:29:38

问题


I am trying to build a tableview cell that include videos in it. I am keeping my cell in nscache as I saw in another post in stack overflow. There goes my code:

Firstly I am calling this in viewDidload() and when index path.row %5=0

cache = NSMutableDictionary()

do {



            // create post request
            let url = NSURL(string: "http://molocate.elasticbeanstalk.com/video/api/explore/all/")!
            let request = NSMutableURLRequest(URL: url)
            request.HTTPMethod = "GET"

            // insert json data to the request
            //print(userToken)
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.addValue("application/json", forHTTPHeaderField: "Accept")
            request.addValue("Token "+userToken!, forHTTPHeaderField: "Authorization")


            //                let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in

            let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ (data, response, error) -> Void in

                dispatch_async(dispatch_get_main_queue(), {
                    //print(response)
                    //print(NSString(data: data!, encoding: NSUTF8StringEncoding))

                    if error != nil{
                        print("Error -> \(error)")

                        //return
                    }

                    do {
                        let result = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)

                        print("Result -> \(result)")
                        let videos = result["results"] as! NSArray


                        if result["next"] is NSNull {
                            print("next is null")
                            self.nextURL = nil
                        }else {
                            let nextStr = result["next"] as! String
                            print(nextStr)
                            self.nextURL = NSURL(string: nextStr)
                            }
                        for item in videos {

                            var videoStr = videoInf()
                            let urlString = item["video_url"] as! String
                            let url = NSURL(string: urlString)
                            videoStr.urlSta = url!





                            let username = item["current_username"] as! String
                            videoStr.username = username
                            self.videoArray.append(videoStr)


                        }





                    } catch {
                        print("Error -> \(error)")
                    }
                     self.tableView.reloadData()

                })


            }
            task.resume()





        } catch {
            print(error)


        }

Then, There is my code about my table view cell

var cell = videoCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myIdentifier")
     if((cache.objectForKey(NSString(format: "key%lu", indexPath.row))) != nil){
        cell = cache.objectForKey(NSString(format: "key%lu", indexPath.row)) as! videoCell
        cell.player.play()




     }else{



        cell.tableVideoURL = videoArray[indexPath.row].urlSta
        cell.exploreAddView()

     }   

But When I am at the 14th video my app is giving me a memory error and I could not to any utility of my app and the other cell have a place but they are not giving a video.

来源:https://stackoverflow.com/questions/35807327/video-tableviewcell-cause-memory-error-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!