2016-04-25 60 views
0
import UIKit 
import Alamofire 
import SwiftyJSON 

class RecentAdded: UIViewController ,UITableViewDataSource,UITableViewDelegate{ 

    @IBOutlet var tableView: UITableView! 
    var list:JSON! 
    var sendurl:String! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     Alamofire.request(.GET, "http://api.dirble.com/v2/stations/recent", parameters: ["token": "260674ecb51572a8faa4e77199"]) 
     .responseJSON { response in 
      if let json = response.result.value { 
       self.list = JSON(data: response.data!) 
       print(self.list) /// Showing less element if element is more than 25 
       self.tableView.dataSource = self 
       self.tableView.delegate = self 
       self.tableView.reloadData() 
       print(self.list.arrayValue.capacity) // Printing the actual capacity 
      } 
     } 

     // Do any additional setup after loading the view. 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.list.arrayValue.capacity 

    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! RecentCellTableViewCell 


     let sampleArray = self.list.array 
     let imageURL:String! = sampleArray![indexPath.row]["image"]["thumb"]["url"].stringValue 
     if imageURL != ""{ 
      Alamofire.request(.GET, imageURL).responseImage { (response) -> Void in 
       guard let image = response.result.value else { return } 
       cell.img!.image = image 
      } 

     }else{ 
      cell.img!.image = UIImage(named: "rad")! 
     } 

     cell.nm?.text = sampleArray![indexPath.row]["name"].stringValue 
     let catarr = sampleArray![indexPath.row]["categories"].array 
     let des:String! = "category : " + catarr![0]["title"].stringValue + " " + "slug : " + catarr![0]["slug"].stringValue 
     cell.des?.text = des 

     return cell 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     let cell = tableView.cellForRowAtIndexPath(indexPath) as! RecentCellTableViewCell 
     let sampleArray = self.list.array 
     let url = sampleArray![indexPath.row]["streams"].array 
     sendurl = url![0]["stream"].stringValue 
     self.performSegueWithIdentifier("next", sender: self) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // 
     if (segue.identifier == "next") { 
      // initialize new view controller and cast it as your view controller 
      var viewController = segue.destinationViewController as! Player 
      viewController.urll = sendurl 

     } 

    } 
} 

我的問題是當我打印list.arrayvalue.capacity它顯示數組的實際大小是正確的,但是當我試圖打印數組的元素時,它顯示的元素較少,然後它的計數。所以我不知道代碼中出了什麼問題????/使用swiftyjson時數組列表項數和數組元素數不匹配?

主要問題是打印元素。不打印所有元素。

+1

這是非常昂貴的使用(SWIFTY)JSON對象作爲數據源陣列。該對象必須一直在'numberOfRowsInSection'和'cellForRowAtIndexPath'中轉換。根據陣列的大小,您可能會失去性能。 – vadian

+0

謝謝你的建議,我會更新我的代碼。 –

回答

2

我認爲你會混淆數組容量和實際的物品數量。對於numberOfRowsInSection,使用數組,而不是count屬性:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.list.arrayValue.count 
} 

更多關於這個答案數與容量的詳細信息:Swift array.capacity vs array.count

+0

非常感謝你Jere它解決了我的問題。但是我在另一個ViewControle中使用了list.arrayValue.capacity,其中的項目少於25,它工作正常。爲什麼? –

+0

容量和數量之間有什麼區別? –

+0

有關計數與容量的詳細信息,請參閱我答案底部的鏈接。 –