Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a UITableView programmatically to a UIViewController

I'm loading a UIViewController into one of my Nav controller's hierarchies, which will contain some text and some images. At the bottom, I will want to create a expandable and collapsable tableview.

First off, is this idea possible? If it is, how do I add it and where do I place the data source and delegate methods?

Can I just make a separate subclass of the TableViewController and then add it to my ViewController as a subview?

like image 756
Adam Avatar asked Aug 22 '11 20:08

Adam


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.

How do I add a search bar to UITableView?

Adding Search Bar on TableViewAdd the UISearchBar at the top of your UITableView. … and give the name searchBar. In the ViewController add a Boolean called searching to know when to switch between the full list of items and the list of items from the searching results.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

Yes, you can create a UITableView whose delegate, datasource, and parent view are not necessarily a UITableViewController. Since the UITableView is a UIView, you can add it as a subview of any other UIView. Any NSObject can be the delegate or datasource, as long as you implement the required protocol methods.

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

In fact, in my experience, not many people even use UITableViewControllers. When was the last time you wanted your table view to take up the entire usable space? In general, I create a plain old UIViewController and add a UITableView as a subview of its view, in addition to other subviews.

like image 62
commanda Avatar answered Nov 10 '22 01:11

commanda


/************************************************/
/************* MyCustomController.m *************/
/************************************************/

@interface MyCustomController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MyCustomController

- (id)initWithNibName:(NSString*)nibName bundle:(NSString*)bundleName
{
   self = [super initWitNibName:nibName bundle:bundleName];
   if (self)
   {
       self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
       tableView.datasource = self; 
       tableView.delegate = self;
       [self.view addSubview:self.tableView];
   }

   return self;
}

#pragma mark - UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // return number of rows
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // return cell
}

#pragma mark - UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // handle table view selection
}

@end
like image 42
3lvis Avatar answered Nov 09 '22 23:11

3lvis