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,
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.
A view that presents data using rows in a single column.
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.
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.
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
Set table delegate
and dataSource
:
override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With