27

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,

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
serdar aylanc
  • 1,307
  • 2
  • 20
  • 37

4 Answers4

56

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
Greg
  • 25,317
  • 6
  • 53
  • 62
17

Set table delegate and dataSource :

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
0

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.

OriDev
  • 1
  • 3
0

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;
        }
    }
giacoder
  • 980
  • 8
  • 21