2014-10-31 45 views
0
import UIKit 

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 

    var collectionView: UICollectionView? 

    var instanceOfCustomObject: CustomObject = CustomObject() 
    var accessToken: NSString! 
    var userDefaults: NSUserDefaults! 

    var responseDictionary: NSDictionary! 
// var currentResponse: NSArray! 


    // the range represents integer values 0, 1, and 2 
    // the range now represents integer values 6, 7, and 8 

    let colorWheel = ColorWheel() 
// let parse = Parse() 

// var currentResponse: NSArray? 
// struct photoCount { 
// init(responseDictionary: NSDictionary) { 
// 
//  //  var currentResponse = responseDictionary["data"] as NSDictionary 
// 
//  var currentResponse = responseDictionary.valueForKeyPath("data") as NSArray 
//  var photoCount = currentResponse.count as Int 
//  println(photoCount) 
// } 
// } 
    var photoCount: Int! 

// private let api = "d4984c8cfa78689bd066d82bec820fd5" 



    override func viewDidLoad() { 
     super.viewDidLoad() 

     userDefaults = NSUserDefaults.standardUserDefaults() 
     self.accessToken = userDefaults!.objectForKey("accessToken") as NSString 
     println(self.accessToken) 

     //  instanceOfCustomObject.someProperty = "Hello World" 
//  var accessToken : NSString? = NSString(instanceOfCustomObject.accessToken) 
//  println(accessToken) 
//  instanceOfCustomObject.authorize() 
//  instanceOfCustomObject.simpleAuth() 


     // Do any additional setup after loading the view, typically from a nib. 
     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
//  layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0) 
     layout.itemSize = CGSize(width: 124, height: 124) 
     layout.minimumInteritemSpacing = 1.0 
     layout.minimumLineSpacing = 1.0 
     collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
     collectionView!.dataSource = self 
     collectionView!.delegate = self 
     collectionView!.registerClass(Cell.self, forCellWithReuseIdentifier: "Cell") 
     collectionView!.backgroundColor = UIColor.whiteColor() 
     self.view.addSubview(collectionView!) 

     getData() 

    } 

    func getData() -> Void { 
     let baseUrl = NSURL(string:"https://api.instagram.com/v1/users/3/media/recent/?access_token=\(self.accessToken)") 

     let forcastUrl = NSURL(string: "", relativeToURL: baseUrl) 

     //  let data = NSData(contentsOfURL: forcastUrl) 
     //  println(data) 
     let sharedSession = NSURLSession.sharedSession() 
     let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(baseUrl, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in 


      //   var urlContents = NSString.stringWithContentsOfURL(location, encoding: NSUTF8StringEncoding, error: nil) 
      //   println(urlContents) 

      let dataObject = NSData(contentsOfURL: baseUrl) 
      //   println(dataObject) 
      if (error == nil) { 
       let responseDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary 

       var currentResponse = responseDictionary.valueForKeyPath("data") as NSArray 

       //    println(currentResponse) 
       //    self.currentResponse = currentResponse as NSArray 
       self.photoCount = currentResponse.count as Int 
       println(self.photoCount) 

       dispatch_async(dispatch_get_main_queue(), {() -> Void in 

        //     self.temperatureLabel.text = "\(currentWeather.temperature)" 
        //     self.iconView.image = currentWeather.icon! 
        //     self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is" 
        //     self.humidityLabel.text = "\(currentWeather.humidity)" 
        //     self.percipitationLabel.text = "\(currentWeather.precipProbability)" 
        //     self.summeryLabel.text = "\(currentWeather.summary)" 
        // 
        //     self.refreshActivityIndicator.stopAnimating() 
        //     self.refreshActivityIndicator.hidden = true 
        // 
        //     self.refreshButton.hidden = false 
       }) 

      } else { 

       let networkIssueController = UIAlertController(title: "Error", message: "I got 99 problems but a bitch ain't one", preferredStyle: .ActionSheet) 
       let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil) 
       networkIssueController.addAction(okButton) 
       let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 
       networkIssueController.addAction(cancelButton) 

       self.presentViewController(networkIssueController, animated: true, completion: nil) 

       dispatch_async(dispatch_get_main_queue(), {() -> Void in 
        //Stop refresh animation 
        //     self.refreshActivityIndicator.stopAnimating() 
        //     self.refreshActivityIndicator.hidden = true 
        //     self.refreshButton.hidden = false 

       }) 

      } 

     }) 

     downloadTask.resume() 

    } 

在getData()函數中,名爲「currentResponse」的變量檢索一個urls數組。那麼我算陣列和號碼分配給一個名爲「光子計數」函數內的訪問變量,並將其作爲另一個函數的返回swift

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 


//  var photoCount = parse.photoCount(currentResponse) 
     return photoCount 
    } 

我想用「PHOTOCOUNT」變量來告訴我的集合視圖功能,它應該有多少段具有可變!有20個網址,所以它應該輸出20個方格,但目前的photoCount是零!我是否需要將「photoCount」設置爲全局變量?一直困在這個很長時間,請幫助!

回答

0

您收藏視圖的大小分配爲當您的視圖控制器加載並且不會隨時更新,任何地方。您必須異步重新加載您的集合視圖。

在你的getData功能,把這個:

dispatch_async(dispatch_get_main_queue(), { 
    self.photoCount = currentResponse.count as Int 
    self.collectionView.reloadData()   
}) 
+0

Xcode中說,當展開是PHOTOCOUNT爲零。 – alemac852 2014-10-31 08:08:18

相關問題