Swift - UIViewController里面的UITableView,不调用UITableView函数 [英] Swift - UITableView inside UIViewController, UITableView functions are not called

查看:117
本文介绍了Swift - UIViewController里面的UITableView,不调用UITableView函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIViewController中插入了一个tableview。但我的代码不起作用。当我检查时,我发现没有调用任何tableview函数。

I inserted a tableview inside a UIViewController. But my code is not working. When I checked I found that none of the tableview functions are not called.

    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)
        }

任何解决方案?

谢谢,

推荐答案

您必须将视图控制器设置为表视图委托/数据源:

You have to set your view controller as a table view delegate/datasource:

添加到 viewDidLoad 的末尾:

authorArticlesTableView.delegate = self
authorArticlesTableView.dataSource = self

这篇关于Swift - UIViewController里面的UITableView,不调用UITableView函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆