2017-03-16 46 views
0
import UIKit 

let cellid = "cell" 

class Post { 
    var videoName: String? 
    var videoDescription: String? 
} 
class VideoFeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    var posts = [Post]() 
    var json: [Any]? 
    var names: [String] = [] 
    var contacts: [String] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 



     let CatagoryMain = Post() 
     CatagoryMain.videoDescription = "example text" 
     CatagoryMain.videoName = "wewewewew" 

     posts.append(CatagoryMain) 

     collectionView?.backgroundColor = UIColor.white 
     navigationItem.title = "Main Video Feed" 
     collectionView?.alwaysBounceVertical = true 
     collectionView?.register(VideoFeedCell.self, forCellWithReuseIdentifier: cellid) 

     let urlString = "example url" 

     let url = URL(string: urlString) 
     URLSession.shared.dataTask(with:url!) { (data, response, error) in 
      if error != nil { 
       print("failed") 
      } else { 
       do { 

        let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any] 
        let currentVideo = parsedData["video"] as! [String:Any] 


        if let currentVideoTitle = currentVideo["title"] as? String { 
          print(currentVideoTitle) 

         // have text display instead of wewewew and example text 
        } 

       } catch let error as NSError { 
        print(error) 
       } 
      } 

      }.resume() 
    } 

所以现在它说视频描述的示例文本和我们wewewewew视频名称和那些显示罚款。我试图从json获取信息,虽然所以而不是示例文字那就说明和代替wewewewew那就我认为这会是这样的在Swift中不显示JSON文本

if let currentVideoTitle = currentVideo["title"] as? String { 
         print(currentVideoTitle) 

        CatagoryMain.videoDescription = "\(currentVideoTitle)" 
        CatagoryMain.videoName = "\(currentVideoTitle)" 

     posts.append(CatagoryMain) 
       } 

然而这是错误的,视频名称,如果我这样做,没有任何显示在屏幕上,除了导航栏和标题为导航栏。如果您可以提供帮助,请提前致谢!顺便说一下,实际的标题确实显示在控制台中。

+0

所以有行'print(currentVideoTitle)'的任何输出?还可以发布'currentVideo' json吗?那太好了:) –

回答

0

像我上面留下的评论,我们需要更多的代码来弄清楚情况并帮助你:)而你的问题有点不清楚。

但是,这是错误的,如果我这样做,除了导航栏和导航栏的标题之外屏幕上没有任何内容显示 。

您的意思是说UICollectionView正在加载数据?或者数据没有加载到posts数组中?是collectionView甚至初始化?

而且,想看看该parsedData对象。因为您正在使用collectionView这意味着您要显示多个单元格。但是,你的解析代码看上去并不像你解析对象的数组。(但只有一个对象?)

快速意见, 1.打印出posts.counts,看是否有数据? 2.如果有任何数据,在将数据添加到posts后添加collectionView.reloadData()