swift2 视图控制器是否符合协议“UITableViewDataSource,视频模式委托”?

fhg3lkii  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(160)
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

}

}
[附加错误][错误] 1如何使我的viewcontroller符合协议“UITableViewDataSource和视频模式委托”?
我已经尝试了一些建议的变化,在以前的线程,但没有让我通过,请帮助我。
先谢谢你

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

如果您想查看更多的视频,请点击这里。我的天啊
let videoThumbnailUrl!= nil***错误-'!=不是前缀一元运算符'和'模式中缺少类型注解'***
{

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

()}}

dba5bblo

dba5bblo1#

看起来您还没有关闭方法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
    }
}

相关问题