【发布时间】:2015-07-17 09:30:09
【问题描述】:
我使用 alamofire 将 JSON 中的类别名称存储在一个数组中。
数组只有在被这个方法CategoryNameFunc调用时才有值。
如果我从 tableview 或任何其他方法调用数组,它总是 returns 0
代码
var CategoryNameArray : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
Network()
tester()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return CategoryNameArray.count // This returns 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = self.TableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
println(self.CategoryNameArray[indexPath.row])
cell.textLabel?.text = "Hello"
return cell
}
func Network(){
Alamofire.request(.GET, "http://www.wive.com/index.php/capp/category_list")
.responseJSON { (_, _, data, _) in
let json = JSON(data!)
let count = json.count
self.CategoryNameFunc(json, Count: count) }
}
func CategoryNameFunc(Json: JSON, Count: Int)
{
for index in 0...Count-1 {
let name = Json[index]["CATEGORY_NAME"].string
CategoryNameArray.append(name!)
}
// This returns 23 (The correct value)
println(self.CategoryNameArray.count)
}
【问题讨论】:
-
stackoverflow.com/help/how-to-ask 有礼貌,不要使用!如果您需要其他人的帮助。
-
@robertvojta 对不起。我已将其删除...
-
只需将 CategoryNameFunc 中的 println 替换为 self.tableView.reloadData()。通知 tableView 有关更改 - 重新加载。
标签: ios arrays swift swift-playground