Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interact with UITableView in UIViewController?

I am addicted to use TableViewController in storyboard whose class directly inherits from UITableViewController and functions like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

are there by default and you just have to implement them. But now I am using a ViewController (inherits from UIViewController) in storyboard and using a UITableView control from Object library into the view controller by drag and drop. Obviously the class I would attach to my storyboard ViewController will be inherited from UIViewController. Now how do I interact with the table cells in the UITableView control in my view controller. Want to implement same row functions above.

like image 891
Firdous Avatar asked Feb 21 '12 10:02

Firdous


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 class does UITableView inherit from?

The appearance of the tableview is managed by UITableView class, which inherits UIScrollView. In tableview, the row is simulated by the object of the UITableViewCell class, which can be used to display the actual content.

What is IndexPath UITableView?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.

What is the difference between UITableView and UICollectionView?

Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic. 2. UICollectionView is the model for displaying multidimensional data .


2 Answers

Implement UITableViewDataSource and UITableViewDelegate protocols in your view controller .h file.

Have an outlet for the UITableView.

In viewDidLoad, set the tableView's datasource and delegate to self since the viewcontroller implements the protocol.

Then, write the cellForRowAtIndexPath method. It will be getting called.

like image 200
Ilanchezhian Avatar answered Oct 19 '22 22:10

Ilanchezhian


You have to assign UITableViewDelegate and UITableViewDatasource protocols to your table and implement it. Methods in your question are methods from these protocols and they appears automatically when you use UITableViewController.

like image 37
beryllium Avatar answered Oct 19 '22 22:10

beryllium