views:

542

answers:

3

Hi,

Expecting your well advice!

I have a TableView(Grouped Table created via I.B). I'm showing some data on the table when code executes using Navigation Controller view. I want to add a "Tab Bar" in my Table view whether through I.B (or) Programmatically. For ex: Launch built-in "Clock" app, where you can see a Tab Bar which has options of 'World Clock', 'Alarm', 'Stop Watch' and 'Timer' in single view itself. I tried to create like same by adding a 'Tab Bar' in I.B, but drag and drop of Tab Bar doesn't sit in the Table View. I don't know why. (or) else i even tried programmatically to create it in the Table View, but not succeeded. Is it not possible? Can anyone please help me on pointing out the code or samples?

thanks.

Calve/

A: 

Try creating a UIView and adding a tabBar to that view, and now place this view inside your table. I tried this and its working,

So your function cellForRowAtIndexPath will look like this

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    UITabBar* tabBarView = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    NSArray* tabbarItems = [[NSArray alloc] initWithObjects:[[UITabBarItem alloc] initWithTitle:@"Item1" image:nil tag:1],[[UITabBarItem alloc] initWithTitle:@"Item2" image:nil tag:2],[[UITabBarItem alloc] initWithTitle:@"Item3" image:nil tag:3], nil];
    tabBarView.items = tabbarItems;
    [tempView addSubview:tabBarView];

    [cell addSubview:tempView];


    return cell;
}

So now instead of using default cell you can customize it to the way you want.Also I havent considered memory release issue so u might need to update the code to release the allocated memory.

rkb
Thanks a lot. Your code works. But having another problem: Created a tab bar has fit with table row, so whenever i scroll down the table view contents, Tab bar also comes down with that. I want Tab bar to be worked separately and being permanent in down, not combined with Table Row items.
Clave Martin
Well then you can do the same thing but add it to the view instead of the cell. Like I am adding the view to the cell, instead add the view to you home page view with whtever location you want.
rkb
Thanks. I checked that already by inputting the code snippet like below, but the same problem observed.- (void)viewDidLoad { [super viewDidLoad]; // Our same code here to create Tab Bar ..................................... [tempView addSubview:tabBarView]; [self.view addSubview:tabBarView];}Any advice please.
Clave Martin
Sorry the last line should be read as " [self.view addSubview: tempView];"
Clave Martin
Hey sorry for the delay, I am not sure why is it not working but you can always do one thing as having multiple tables and locking the scrolling for the table which has tabBar.
rkb
+1  A: 

I think you are taking the wrong approach. Adding a tab bar to your table view is wrong. You are supposed to start with the tab bar app and add a navigation controller to that tab bar app. Inside the navigation controller you can add a view with a table on it.

marcc
A: 

You need to add it to your parent controller's view. Also the tempView element is not necessary.

E.g. if the app uses a navigation controller as the main view controller you would add this to your custom UITableViewController class:

- (void)viewDidLoad
{
    [super viewDidLoad];    UITabBar* tabBarView = [[UITabBar alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
    NSArray* tabbarItems = [[NSArray alloc] initWithObjects:[[UITabBarItem alloc] initWithTitle:@"Item1" image:nil tag:1],[[UITabBarItem alloc] initWithTitle:@"Item2" image:nil tag:2],[[UITabBarItem alloc] initWithTitle:@"Item3" image:nil tag:3], nil];
    tabBarView.items = tabbarItems;
    [self.navigationController.view addSubview:tabBarView];
}