tags:

views:

4206

answers:

4

I am using a tab bar (UITabBarController) on my app and I wish to customize the appearance of the table that appears when you click the more button. I have worked out how to change the appearance of the Navigation bar that is on the more screen by setting

self.moreNavigationController.navigationBar.barStyle

in a subclass of UITabBarController and I have managed to change the background colour of the table by modifying

self.moreNavigationController.topViewController.view.backgroundColor

, but I cannot work out how to change the font colour in the cells that appear on the table. I was hoping I could use

self.moreNavigationController.topViewController.view.visibleCells

but this always seems to be empty. I've tried doing this in viewDidLoad, viewWillAppear and viewDidAppear with no success. The object self.moreNavigationController.topViewController is of type UIMoreListController, which seems to be undocumented and I can't see anything obvious in the interface that will help me.

Any ideas?

+4  A: 

visibleCells is populated only after the moreNavigationController is displayed.

And the cells are created at runtime, so even if you change the content of the cells, they are replaced when they are displayed.

One thing to try would be to replace the datasource of the moreNavigationController tableView, call the cellForRowAtIndexPath of the original datasource and change its content before returning it.

Using the code below, after having displayed once the moreNavigationController to initialize it, you'll see that when you return to the moreNavigationController, the cells are red, but return immediately to white background.

UITableView *view = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
if ([[view subviews] count]) {
  for (UITableViewCell *cell in [view visibleCells]) {
    cell.backgroundColor = [UIColor redColor];
  }
}
Stephan Burlot
Good suggestion. i am going to try it
Ian1971
It works. I am new to this site, should I post my implementation of your idea as a separate answer? What is the correct protocol?
Ian1971
I would suggest to edit your own question and add the answer below. But I'm a newbie too!
Stephan Burlot
+3  A: 

Following on from Stephan's suggestion to replace the dataSource of the moreNavigationController, here is a quick over view of the code I implemented.

I created a new class called MoreTableViewDataSource which implements the UITableViewDataSource protocol. The controller which the more page actually uses to build the table is called the UIMoreListControllerModern, and this implements just the required parts of the UITableViewDataSource protocol. My implementation looks like this.

-(MoreTableViewDataSource *) initWithDataSource:(id<UITableViewDataSource>) dataSource
{
    originalDataSource = dataSource;
    [super init];

    return self;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return [originalDataSource tableView:table numberOfRowsInSection:section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.textColor = [UIColor whiteColor];
    return cell;
}

and then in my CustomTabBarController class I override viewDidLoad as follows:

- (void)viewDidLoad {

    [super viewDidLoad];

    UINavigationController *moreController = self.moreNavigationController;
    moreController.navigationBar.barStyle = UIBarStyleBlackOpaque;

    if ([moreController.topViewController.view isKindOfClass:[UITableView class]])
    {

        UITableView *view = (UITableView *)moreController.topViewController.view;
        view.backgroundColor = [UIColor blackColor];
        moreTableViewDataSource = [[MoreTableViewDataSource alloc] initWithDataSource:view.dataSource];
        view.dataSource = moreTableViewDataSource;

    }
}

As requested here are the header files

@interface MoreTableViewDataSource : NSObject <UITableViewDataSource>
{
    id<UITableViewDataSource> originalDataSource;
}

@property (retain) id<UITableViewDataSource> originalDataSource;

-(MoreTableViewDataSource *) initWithDataSource:(id<UITableViewDataSource>) dataSource;

@end

and

#import "MoreTableViewDataSource.h"

@interface CustomTabBarController : UITabBarController 
{
    MoreTableViewDataSource *moreTableViewDataSource;
}
Ian1971
Just joining the conversation here … If I understand correctly then, you're chaining in moreTableViewDataSource. Nice! Question though: Should there be a [moreTableViewDataSource release] after the view.dataSource assignment, to balance out the alloc?
Joe D'Andrea
Ahh, wait a sec. moreTableViewDataSource isn't accessed via a property, so there's no extra retain, right? But I wonder if this should be a property anyway … hmm ...
Joe D'Andrea
A: 

Hi! What does the interface file look like?

I think it was fairly standard, nothing special related to this.
Ian1971
A: 

Hi ian1971

I'm new to iPhone programming, can you provide the complete implementation and header files of your MoreTableViewDataSource and CustomTabBarController classes? thanks!

Marvzz
I added header files above
Ian1971
Thanks for this! :) still going to try this, though i already resorted to not use the MoreNavigationController.
Marvzz