我在 UIViewController 中插入了一个 TableView 。但是我的代码不起作用。当我检查时,我发现没有一个 tableview 函数没有被调用。
class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var authorArticlesTableView: UITableView!
var authorid: Int!
var authorname: String!
var articles: [JSON]? = []
func loadArticles(){
let url = "http:here.com/" + String(authorid) + "/"
println(url)
Alamofire.request(.GET, url).responseJSON { (Request, response, json, error) -> Void in
if (json != nil){
var jsonObj = JSON(json!)
if let data = jsonObj["articles"].arrayValue as [JSON]?{
self.articles = data
self.authorArticlesTableView.reloadData()
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadArticles()
println("viewDidLoad")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
println("numberOfRowsInSection")
return self.articles?.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell
cell.articles = self.articles?[indexPath.row]
println("cellForRowAtIndexPath")
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("WebSegue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
有什么解决办法吗?
谢谢,
最佳答案
您必须将您的 View Controller 设置为 TableView 委托(delegate)/数据源:
添加到 viewDidLoad
的末尾:
authorArticlesTableView.delegate = self
authorArticlesTableView.dataSource = self
关于ios - Swift - UIViewController 内的 UITableView,不调用 UITableView 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30576291/