Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 261
serdar aylanc Avatar asked Jun 01 '15 14:06

serdar aylanc


People also ask

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What is UITableView in Swift?

A view that presents data using rows in a single column.

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.

What is Indexpath in Tableview Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.


2 Answers

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 
like image 121
Greg Avatar answered Sep 27 '22 21:09

Greg


Set table delegate and dataSource :

override func viewDidLoad() {     super.viewDidLoad()      tableView.delegate = self     tableView.dataSource = self } 
like image 41
Ashish Kakkad Avatar answered Sep 27 '22 19:09

Ashish Kakkad