Question

Apologies if this question is a little basic but I've spent several days trying to understand the root cause of this problem without any success.

I am working on an app which relies heavily on UITableView objects. I can successfully use a UITableViewController object and display information in a table but I need to be able to have multiple tables on screen citing data from multiple sources and the UITableViewController seems to be too limited.

I would like to be able to place multiple UITableView objects with the storyboard then create custom class files which manage the tables. Unfortunately when I've tried this, XCODE doesn't let me select these custom classes to manage the tables.

Although I've found some potential workarounds online I want to understand why selecting a new class to govern a table view is not possible.

[I wanted to post images but apparently I can´t until I have a better reputation...]

Was it helpful?

Solution

It depends on exactly what you did. But, you should really take a different approach:

If you can, use a single table view with multiple sections (with headers / footers).

If you can't do that, create a separate table view controller and table view for each section of information that you want. Then, your 'main' view controller should act as the parent and add all of the other table view controllers as children (addChildViewController:) and their views as subviews. This approach will keep your code segregated and organised rather than trying to have one controller manage many disparate views.

OTHER TIPS

.h
{
UITableView *objlefttableview;
UITableView *objrighttableview;
}

.m
viewdidload
{

if(!objlefttableview)
        objlefttableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 87, 227, 681) style:UITableViewStylePlain];
    if(!objrighttableview)
        objrighttableview=[[UITableView alloc]initWithFrame:CGRectMake(227, 87, 263, 681) style:UITableViewStylePlain];

  [objlefttableview registerNib:[UINib nibWithNibName:@"View" bundle:Nil] forCellReuseIdentifier:@"leftCell"];

 [objrighttableview registerNib:[UINib nibWithNibName:@"ViewR" bundle:Nil] forCellReuseIdentifier:@"rightCell"];

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

if (self->objlefttableview==tableView)
    {

}

else
{

}

}

so what i just did is i created two tableview objects and then i gave two different custom cell to both of them If u need more help in this approach do ask

The problem was that where as the UITableViewController object is by default the UITableView delegate and it's datasource, a UIView is not even if it inherits from UITableViewController. I hadn’t specified that and it seems that neither 1 nor multiple tables could function as they had no class governing them set to be delegate and data source.

By specifying in the ViewController’s .h file that it was also the delegate and datasource for the UITableView like below (the delegate and datasource commands should be surrounded by triangle brackets but they aren't displayed on this for some reason):

@interface DHViewController : UIViewController [UITableViewDataSource, UITableViewDelegate]

and in the .m file’s viewDidLoad method specify that it was the data source and delegate for both tableViews like so:

self.tableAnswers.delegate =self;
self.tableAnswers.dataSource = self;
self.tableQuestions.delegate   =self;
self.tableQuestions.dataSource = self;

and implementing the necessary methods in the .m file:

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

…both tables can be independently managed and displayed on the same screen.

Thanks a lot to all for your help!

FYI (I think I will still go for @Wain ’s idea of having a single table with section specific content/behaviour - it seems much neater).

1) Add UITableView for your storyboard.

2) Set delegate and data source.

3) Create Outlets (properties in your class) for those tableView's

Then you can work with thouse table views. For example, place label on it, and change it text dynamically in your programm.

Cheers :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top