我正在使用SwiftyJson和Alamofire访问JSON。我把我的产品名称放到一个数组中,但是当我试图访问它的UITableView
时:product_name[0]
,我从标题中收到错误(索引超出范围)。这是代码:
let total_products:Int = json["products"].count;
var i:Int = 0;
for (i=0;i<total_products;i++)
{
product_name.append(json["products"][i])
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = product_name[indexPath.row];
return cell
}
要避免超出范围错误,请确保numberOfRowsInSection
返回的UITableViewDataSource
是数组中元素的数量。
return product_name.count
我做到了!
我在self.tableView.reloadData();
循环中添加了for
,现在我的TableView
已经填充了。
感谢你们 !