iOS 5, storyboards: customize programmatically created UITableView for every UIView

瘦欲@ 提交于 2019-12-12 00:10:02

问题


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.



来源:https://stackoverflow.com/questions/12042770/ios-5-storyboards-customize-programmatically-created-uitableview-for-every-uiv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!