私は自分の仕事のウェブサイトを補完するための求人アプリの作成を開始したばかりですが、私たちが持っていない場所ジョブ。私はコンテンツを取得することができますが、私はそれを辞書に格納してコレクションビューのセルを作成するために利用することはできません。ビューコントローラのための私のコードは以下であり、任意のアドバイスは非常に多くのチュートリアルとウェブ検索の後として評価されています...私は、私は完全にそれを破ったと思う:JSONを解析してCollectionViewCell Swift 2に表示する
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var myCollectionView: UICollectionView!
var jobtitle = [[String: String]]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadJobs()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(jobtitle.count)
return jobtitle.count
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//MyCollectionViewCell
let myCell: MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as! MyCollectionViewCell
let jobTitleString = self.jobtitle[indexPath.row] as? [String:String]
let trueJobTitle = jobTitleString!["jobtitle"] // as! String
myCell.jobTitleLabel.textColor = UIColor.whiteColor()
myCell.jobTitleLabel.text = trueJobTitle
return myCell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("User tapped on: \(indexPath.row)")
}
func loadJobs() {
let startTime = NSDate.timeIntervalSinceReferenceDate()
var pageUrl = "http://api.indeed.com/ads/apisearch?publisher=8134512135661512&format=json&q=java&l=austin%2C+tx&sort=date&radius=25&st=&jt=&start=&limit=10&fromage=&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29&v=2"
let myUrl=NSURL(string: pageUrl)
let request = NSMutableURLRequest(URL:myUrl!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
// If error display alert message
if error != nil {
var myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default
, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
do {
let jsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSArray
if let parseJSONArray = jsonArray
{
self.jobtitle = parseJSONArray as! [[String:String]]
dispatch_async(dispatch_get_main_queue(), {self.myCollectionView.reloadData()});
}
} catch {
print("error: \(error)")
}
print(self.jobtitle)
}
task.resume()
}
}
'print(self.jobtitle)'は何も印刷しませんか?基本的にコードはOKですが、テキストの色とセルの背景色が両方とも白であれば、何も表示されません;-)。 'jobtitle'は' non-optional delete 'として宣言されているので'? [String:String] 'cellForItemAtIndexPath'の中に挿入し、感嘆符を閉じ括弧の後ろの次の行に置きます。 – vadian
print(self.jobtitle)はそこにしかないので、何かがxcodeに置かれているかどうかを確認することができます。これは本番環境では削除されます。走っているときの背景色は黒で、テキストを白くしてみました。 – user1761348