Swift - UIViewController里面的UITableView,不调用UITableView函数

ser*_*anc 25 uitableview ios swift

我在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)
        }
Run Code Online (Sandbox Code Playgroud)

对此有何解决方案?

谢谢,

Gre*_*reg 52

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

加到最后viewDidLoad:

authorArticlesTableView.delegate = self
authorArticlesTableView.dataSource = self
Run Code Online (Sandbox Code Playgroud)


Ash*_*kad 16

设置表格delegatedataSource:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}
Run Code Online (Sandbox Code Playgroud)