viewcontroller 是不是符合“UITableViewDataSource, Video Mode Delegate”协议?
Posted
技术标签:
【中文标题】viewcontroller 是不是符合“UITableViewDataSource, Video Mode Delegate”协议?【英文标题】:viewcontroller conform to protocol "UITableViewDataSource, Video Mode Delegate"?viewcontroller 是否符合“UITableViewDataSource, Video Mode Delegate”协议? 【发布时间】:2016-03-24 09:38:45 【问题描述】:import UIKit
import Alamofire
protocol VideoModelDelegate
func dataReady()
class VideoModel: NSObject
let API_KEY = ""
let UPLOADS_PLAYLIST_ID = ""
var videoArray = [Video]()
var delegate:VideoModelDelegate?
func getFeedVideos() -> [Video]
Alamofire.request(.GET, "",parameters: ["part":"snippet", "playlistId":UPLOADS_PLAYLIST_ID ,"key":API_KEY],
encoding: ParameterEncoding.URL, headers: nil).responseJSON (response) -> Void in
if let JSON = response.result.value
var arrayOfVideos = [Video]()
for video in JSON["items"] as! NSArray
print(video)
let videoObj = Video()
videoObj.videoId = video.valueForKeyPath("snippet,resourceId, videoId")
String
videoObj.videoTitle = video.valueForKeyPath("snippet.title") as! String
videoObj.videoDescription = video.valueForKeyPath("snippet.description") as!
String
videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.maxres.url") as! String
arrayOfVideos.append(videoObj)
self.videoArray = arrayOfVideos
if self.delegate != nil
self.delegate!.dataReady()
func getVideos() -> [Video]
var videos = [Video]()
let video1 = Video()
video1.videoId = ""
video1.videoTitle = ""
videos.append(video1)
return videos
[Additional Errors][Error]1如何让我的viewcontroller符合“UITableViewDataSource, and Video Mode Delegate”协议?
我在之前的帖子中尝试了一些建议的更改,但没有任何帮助,请帮助我。
提前致谢
import UIKit
import Alamofire
类选项卡:UIViewController、UITableViewDataSource、 UITableViewDelegate、VideoModelDelegate 错误 - 类型“选项卡”不符合协议“UITableViewDataSource”
@IBOutlet weak var table: UITableView!
@IBOutlet weak var name: UILabel!
var videos : [Video] = [Video]()
var selectedVideo:Video?
let model:VideoModel = VideoModel()
var arrRes = [[String:AnyObject]]() //Array of dictionary
override func viewDidLoad()
super.viewDidLoad()
self.model.delegate = self
//self.videos = model.getVideos()
model.getFeedVideos()
self.table.dataSource = self
self.table.delegate = self
/*Alamofire.request(.GET, "http://online.m-tutor.com/mtutor/gateway/mtutorAPI_1.php?type=university").response (req, res, data, error) -> Void in
print(res)
let outputString = NSString(data: data!, encoding:NSUTF8StringEncoding)
print(outputString)
Alamofire.request(.GET, "http://online.m-tutor.com/mtutor/gateway/mtutorAPI_1.php?type=university").responseJSON response in
if let swiftyJsonVar = response.data
print("swiftyJsonvar:\(swiftyJsonVar)")
*/
Alamofire.request(.GET, "http://online.m-tutor.com/mtutor/gateway/mtutorAPI_1.php?type=university").responseJSON (responseData) -> Void in
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["contacts"].arrayObject
self.arrRes = resData as! [[String:AnyObject]]
if self.arrRes.count > 0
self.table.reloadData()
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func dataReady()
self.videos = self.model.videoArray
self.table.reloadData()
func tableView(table: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) ->
CGFloat
return(self.view.frame.size.width / 320) * 180
func tableview(table:UITableView, numberOfRowsInSection section: Int ) ->Int
return videos.count
func tableVie(table: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell
let cell = table.dequeueReusableCellWithIdentifier("jsonCell")!
let videoTitle = videos[indexPath.row].videoTitle
let label = cell.viewWithTag(2) as! UILabel
label.text = videoTitle
//cell.textLabel?.text = videoTitle
让 videoThumbnailUrlString = 视频[indexPath.row].videoThumbnailUrlString;)ERROR:Expected Expression
let videoThumbnailUrl != nil ERROR-'!= 不是前缀一元运算符' 和“模式中缺少类型注释”
let request = NSURLRequest(URL: videoThumbnailUrl!)
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: (data:NSData?, response:NSURLResponse?, error:NSError) -> Void in
dispatch_async(dispatch_get_main_queue(), () -> Void in
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = UIImage(data: data!)
)
)
dataTask.resume()
return cell
func tableView(table: UITableView, didSelectRowAtIndexPath indexpath: NSIndexPath)
self.selectedVideo = self.videos[indexpath.row]
self.performSegueWithIdentifier("toVDVC", sender: self)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
let detailViewController = segue.destinationViewController as! VDViewController
detailViewController.selectedVideo = self.selectedVideo
重新
【问题讨论】:
您的 tableview 数据源委托方法是否在 didReceiveMemoryWarning 方法中? 是的,先生,我已经附上了整个代码,请看一下... 【参考方案1】:看起来好像您还没有关闭方法 didReceivedMemoryWarning: 并且 tableview 委托方法有效地在此方法中导致您的问题。添加一个右括号应该没问题:
更新:这是更新后的代码。在 cellForRowAtIndex 中还有一些括号格式问题和 if 语句错误:
import UIKit
import Alamofire
class tab: UIViewController, UITableViewDataSource, UITableViewDelegate, VideoModelDelegate
@IBOutlet weak var table: UITableView!
@IBOutlet weak var name: UILabel!
var videos : [Video] = [Video]()
var selectedVideo:Video?
let model:VideoModel = VideoModel()
var arrRes = [[String:AnyObject]]() //Array of dictionary
override func viewDidLoad()
super.viewDidLoad()
self.model.delegate = self
//self.videos = model.getVideos()
model.getFeedVideos()
self.table.dataSource = self
self.table.delegate = self
/*Alamofire.request(.GET, "http://online.m-tutor.com/mtutor/gateway/mtutorAPI_1.php?type=university").response (req, res, data, error) -> Void in
print(res)
let outputString = NSString(data: data!, encoding:NSUTF8StringEncoding)
print(outputString)
Alamofire.request(.GET, "http://online.m-tutor.com/mtutor/gateway/mtutorAPI_1.php?type=university").responseJSON response in
if let swiftyJsonVar = response.data
print("swiftyJsonvar:\(swiftyJsonVar)")
*/
Alamofire.request(.GET, "http://online.m-tutor.com/mtutor/gateway/mtutorAPI_1.php?type=university").responseJSON (responseData) -> Void in
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["contacts"].arrayObject
self.arrRes = resData as! [[String:AnyObject]]
if self.arrRes.count > 0
self.table.reloadData()
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
//ADDED CLOSING BRACKET HERE
func dataReady()
self.videos = self.model.videoArray
self.table.reloadData()
func tableView(table: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) ->
CGFloat
return(self.view.frame.size.width / 320) * 180
func tableview(table:UITableView, numberOfRowsInSection section: Int ) ->Int
return videos.count
func tableVie(table: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell
let cell = table.dequeueReusableCellWithIdentifier("jsonCell")!
let videoTitle = videos[indexPath.row].videoTitle
let label = cell.viewWithTag(2) as! UILabel
label.text = videoTitle
//cell.textLabel?.text = videoTitle
//CHANGE THESE TWO LINES:
//let videoThumbnailUrlString = videos[indexPath.row].videoThumbnailUrlString;)
//if let videoThumbnailUrl != nil
//TO THIS:
if let videoThumbnailUrl = videos[indexPath.row].videoThumbnailUrlString
let request = NSURLRequest(URL: videoThumbnailUrl!)
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: (data:NSData?, response:NSURLResponse?, error:NSError) -> Void in
dispatch_async(dispatch_get_main_queue(), () -> Void in
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = UIImage(data: data!)
)
)
dataTask.resume()
return cell
func tableView(table: UITableView, didSelectRowAtIndexPath indexpath: NSIndexPath)
self.selectedVideo = self.videos[indexpath.row]
self.performSegueWithIdentifier("toVDVC", sender: self)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
let detailViewController = segue.destinationViewController as! VDViewController
detailViewController.selectedVideo = self.selectedVideo
【讨论】:
先生,我进行了更改,但错误仍然存在。 Xcode 建议我对这一行进行这些更改“让 videoThumbnailUrl != nil”(建议:1- 一元运算符不能与其操作数分开,2- 行上的连续语句应该用 ';' 分隔)和其他错误仍然存在好像一样。请帮助我... 刚刚发现一些语法问题,请检查更新中的 cmets。 先生,错误计数已减少,截至目前,如果让 videoThumbnailUrl = videos[indexPath.row].videoThumbnailUrlString (ERROR- 'video' 类型的值没有成员',我会收到这两个错误videoThumbnailUrlString' ),另一个错误是“类型'tab'不符合协议'UITableViewDataSource'” 这两个错误是什么。 (对不起,我现在没有 xCode 来测试,只是通过阅读你的代码 sn-ps 来做这个) 1) 'video' 类型的值没有成员 'UITableViewDataSource' 2) 类型 'tab' 不符合协议 'UITableViewDataSource以上是关于viewcontroller 是不是符合“UITableViewDataSource, Video Mode Delegate”协议?的主要内容,如果未能解决你的问题,请参考以下文章
ViewController 不符合自定义 UIButton 类
ios 8.1:类型“ViewController”不符合协议“UICollectionViewDataSource”
类型“ViewController”不符合协议“UITableViewDataSource”