Swift - UITableView inside UIViewController, UITableView functions are not called

痞子三分冷 提交于 2020-01-10 09:34:11

问题


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

Any solution for this?

Thanks,


回答1:


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

add to the end of the viewDidLoad:

authorArticlesTableView.delegate = self
authorArticlesTableView.dataSource = self



回答2:


Set table delegate and dataSource :

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}



回答3:


Sometimes, if you forget to drap and drop UITableViewCell to UITableView. XCode don't understand TableView has Cell. By default, when you drap and drop UITableView into UIViewController. I see UITableView has Cell. But you need to drap and drop UITableViewCell into UITableView also.

It is work with me.




回答4:


Makes sense to do it in your

 @IBOutlet weak var authorArticlesTableView: UITableView!

so it will become

 @IBOutlet weak var authorArticlesTableView: UITableView! {
        didSet {
            authorArticlesTableView.delegate = self;
            authorArticlesTableView.dataSource = self;
        }
    }


来源:https://stackoverflow.com/questions/30576291/swift-uitableview-inside-uiviewcontroller-uitableview-functions-are-not-calle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!