我正在尝试构建一个包含视频的tableview单元格。正如我在堆栈溢出中的另一篇文章中看到的那样,我将我的单元格保留在nscache中。我的代码就是:
首先我在viewDidload()中调用它,当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)
}
然后,有关于我的表格视图单元格
的代码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()
}
但是当我在第14个视频时,我的应用程序给了我一个内存错误,我无法使用我的应用程序的任何实用程序,而其他单元格有一个位置,但他们没有给出视频。