2016-04-25 57 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

有关计数与容量的详细信息,请参阅我答案底部的链接。 –