iOS 5, storyboards: customize programmatically cre

2019-09-09 19:10发布

问题:

In storyboards I've created an UIViewController and added more two UIViews to the same storyboard. Then added UISegmentedControl to enable switch between these UIViews.

The method I use to switch between UIView:

-(IBAction)segmentValueChaged:(id)sender
{
if(self.segment.selectedSegmentIndex==0)
{
    [self.coinageView removeFromSuperview];
    [self.view addSubview:nominalsView];
    [self populateNominals:self.subCountryID];
}
else 
{
    [self.nominalsView removeFromSuperview];
    [self.view addSubview:coinageView];
}
}

And the method which adds a UITableView on an UIView:

-(void)populateNominals:(int)subCountryID
{
UITableView *nominalsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 372) style:UITableViewStylePlain];
//nominalsTableView.dataSource = self;
//nominalsTableView.delegate = self;
[self.nominalsView addSubview:nominalsTableView];
}

This approach works fine and I'me getting a table popped up when switching the segment.

My question is, where can I customize every tableView for every segment (UIView)? The problem is that I need two quite-different-looking tables (different design, cellForRowAtIndexPath and different navigation while didSelectRowAtIndexPath).

Thank you in advance.

回答1:

If you have "two quite-different-looking tables" then set the dataSource and delegate for each tableview to a different object. Meaning create a class/object for each table that will function as the dataSource and delegate for a table. Normally a these are set to 'self' but there is nothing to say they have to be that.

If your lazy and don't care about beautiful reusable code you could just put if/else statements in all the tableView delegate and dataSource calls and and compare the passed in tableView variable to see what table it is. But that is a bit ugly and easy to mess up. Especially if it grows to more than 2 different kinds of tables.