James
James

Reputation: 1177

Add, list and remove items from a UITableView in Swift

I need to create an app that uses a UITableView within a UIView. So I created my scketch the app, and added what he needed. In UIView has a button (which will be used to add items) a tableView (to list items). I created a SubClass of UITableView to add, list and remove items in the tableView.

I tried to add some code like this: func override tableView (tableView: UITableView ?, numberOfRowsInSection section: Int). But the result is a mistake because not being in a UITableViewController.

My question is: How do I add, list and remove items from a UITableView within a UIView, without using a UITableViewController?

Upvotes: 0

Views: 155

Answers (2)

Dave Batton
Dave Batton

Reputation: 8835

@RP's answer is correct for your approach, but your approach is wrong. You almost never want to subclass UITableView. You want to make your UIViewController the UITableViewDataSource and UITableViewDelegate.

Here's a tutorial that describes how you should be adding a table view to a standard view controller.

Upvotes: 3

R P
R P

Reputation: 1193

When you subclass your UITableView, your subclass will have to conform to UITableViewDataSource and UITableViewDelegate protocols (based on your application's requirement). These protocols provide necessary methods for your subclass including but not limited to tableView (tableView: UITableView?, numberOfRowsInSection section: Int) method.

Refer to this Apple tutorial which provides further details. Refer to the "Display the Data" section

Upvotes: 1

Related Questions