【发布时间】:2017-01-26 07:34:35
【问题描述】:
我是 iOS 新手,从一些教程开始。但是当我尝试在tableview 中显示内容时,它没有显示。我所做的一切都像他们在那篇教程中所做的那样。但我仍然无法在表格视图中显示我的内容。
这里是我的教程链接:My link
这是我的代码:
import UIKit
import Alamofire
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var nameArray = [AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Alamofire.request("http://thecodeeasy.com/test/swiftjson.json").responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String,AnyObject>{
if let mainDict = dict["actors"] {
self.nameArray = mainDict as! [AnyObject]
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as? CustomTableViewCell
let title = nameArray[indexPath.row]["title"]
cell?.ContentName.text = title as? String
return cell!
}
}
【问题讨论】:
-
在 swift 3.0 中吗?
-
是的......
-
将 Alamofire.request("thecodeeasy.com/test/swiftjson.json") 写入 viewdidappear 而不是 viewdidload
-
@sinha 您需要在 plist 文件中启用 NSAllowsArbitraryLoads。我只是将您的 API 实现到一个新项目中并发现了这个问题。请查看演示项目和详细信息的答案部分。
标签: ios iphone swift uitableview