serdar aylanc
serdar aylanc

Reputation: 1347

Swift - UITableView inside UIViewController, UITableView functions are not called

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,

Upvotes: 27

Views: 31147

Answers (4)

giacoder
giacoder

Reputation: 980

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

Upvotes: 0

OriDev
OriDev

Reputation: 1

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.

Upvotes: 0

Greg
Greg

Reputation: 25459

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

Upvotes: 56

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

Set table delegate and dataSource :

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

Upvotes: 17

Related Questions