1
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

        }

    }
}

My problem is when i am printing list.arrayvalue.capacity it is showing the actual size of array which is correct but when i tried to print element of array it show's less element then its counting. so i am not sure what is wrong in code????/

The main problem is in printing element. Not printing all elements.

Jere Käpyaho
  • 1,305
  • 1
  • 10
  • 29
Krishna Kumar Thakur
  • 1,456
  • 12
  • 27
  • 1
    It's pretty expensive to use a (Swifty)JSON object as data source array. The object must be converted all the time in `numberOfRowsInSection` and `cellForRowAtIndexPath`. Depending on the size of the array you can get loss of performance. – vadian Apr 25 '16 at 06:56
  • Thank you for the suggestion i will update my code. – Krishna Kumar Thakur Apr 25 '16 at 07:23

1 Answers1

3

I think you're confusing the array capacity with the actual item count. For numberOfRowsInSection, use the count property of the array instead:

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

More details about count vs. capacity in this answer: Swift array.capacity vs array.count

Community
  • 1
  • 1
Jere Käpyaho
  • 1,305
  • 1
  • 10
  • 29