使用 swiftyjson 时数组列表项计数和数组中元素的数量不匹配?
Array list item count and number of element in an array mismatch while using swiftyjson?
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 它显示数组的实际大小是正确的但是当我尝试打印数组元素时它显示的元素少于它的计数。所以我不确定代码有什么问题????/
主要问题出在打印元素上。不打印所有元素。
我认为您将数组容量与实际项目数混淆了。对于numberOfRowsInSection
,使用数组的count
属性代替:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.list.arrayValue.count
}
此答案中有关计数与容量的更多详细信息:
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 它显示数组的实际大小是正确的但是当我尝试打印数组元素时它显示的元素少于它的计数。所以我不确定代码有什么问题????/
主要问题出在打印元素上。不打印所有元素。
我认为您将数组容量与实际项目数混淆了。对于numberOfRowsInSection
,使用数组的count
属性代替:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.list.arrayValue.count
}
此答案中有关计数与容量的更多详细信息: