2017-03-16 45 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()