使用swiftyjson时,数组列表项计数和数组中元素数不匹配?

时间:2016-04-25 06:26:33

标签: arrays swift tableview alamofire swifty-json

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时它显示的数组的实际大小是正确的,但是当我尝试打印数组元素时,它显示的元素少于其计数。所以我不确定代码???? /

有什么问题

主要问题在于打印元素。不打印所有元素。

1 个答案:

答案 0 :(得分:3)

我认为您将阵列容量与实际项目数混淆。对于numberOfRowsInSection,请改为使用数组的count属性:

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

此答案中关于计数与容量的更多详细信息:Swift array.capacity vs array.count