I am unable to change the background color of static UITableViewCells on iOS 7, when running on iPad device. You can easily check this with following setup:
There is a much easier way to override the cell background color. Subclass UITableViewCell and set it as the class for the static tableview cell. Then override:
-(void) willMoveToSuperview:(UIView *)newSuperview {
self.backgroundColor = [UIColor ...];
}
where [UIColor ...] = whatever color you want (e.g. [UIColor clearColor] etc);
Change the ContentView background color instead and it will work.
Do this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *pattern = [UIImage imageNamed:@"image.png"];
[cell setBackgroundColor:[UIColor colorWithPatternImage:pattern]];
}
Work for me on IOS7
The only way I could fix this problem was to make the table programmatically instead of using the storyboard. For reference I will post my solution, I hope it can help anyone.
I replaced the uitableviewcontroller for a uiviewcontroller.
then added this:
the header file
#import <UIKit/UIKit.h>
@interface LtHomeViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *mFiles;
NSArray *mPics;
}
@end
And the module file
#import "LtHomeViewController.h"
#import "LtHomeToolbar.h"
#import "LtHomeCustomCell.h"
@interface LtHomeViewController () <LtHomeToolbarDelegate>
@end
@implementation LtHomeViewController
{
LtHomeToolbar *mainToolbar;
UITableView *theListView;
}
#define TOOLBAR_HEIGHT 64.0f
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect toolbarRect = self.view.bounds; // View controller's view bounds
toolbarRect.size.height = TOOLBAR_HEIGHT;
mainToolbar = [[LtHomeToolbar alloc] initWithFrame:toolbarRect]; // At top
mainToolbar.delegate = self;
[self.view addSubview:mainToolbar];
//
mFiles = [[NSArray alloc] initWithObjects:@"../Lumina.app/lumina0.pdf", @"../Lumina.app/lumina8.pdf", @"../Lumina.app/lumina9.pdf", @"../Lumina.app/lumina10.pdf", nil];
mPics = [[NSArray alloc] initWithObjects:@"vol0.jpg", @"vol8.jpg", @"vol9.jpg", @"vol10.jpg", nil];
//
CGRect tableViewFrame = self.view.bounds;
tableViewFrame.origin.y = TOOLBAR_HEIGHT;
tableViewFrame.size.height = self.view.bounds.size.height - TOOLBAR_HEIGHT;
theListView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
theListView.delegate = self;
theListView.dataSource = self;
theListView.backgroundColor = [UIColor colorWithRed:(116/255.0) green:(167/255.0) blue:(179/255.0) alpha:1.0];
[self.view addSubview:theListView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return [mFiles count];
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HomeCell";
// Similar to UITableViewCell, but
LtHomeCustomCell *cell = (LtHomeCustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[LtHomeCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.image.image = [UIImage imageNamed:[mPics objectAtIndex:indexPath.row]];
NSString *magName = [[mFiles objectAtIndex:indexPath.row]stringByReplacingOccurrencesOfString:@"../Lumina.app/" withString:@""];
magName = [magName stringByReplacingOccurrencesOfString:@"lumina" withString:@""];
magName = [magName stringByReplacingOccurrencesOfString:@".pdf" withString:@""];
magName = [[@"Triathlon LUMINA " stringByAppendingString:magName]stringByAppendingString:@"号"];
cell.descriptionLabel.text = magName;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor colorWithRed:(116/255.0) green:(167/255.0) blue:(179/255.0) alpha:1.0];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
Now I don't know if only this line will help you but as I need more things I took the approach of making the table manually
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor colorWithRed:(116/255.0) green:(167/255.0) blue:(179/255.0) alpha:1.0];
}
David
just write this line
[cell setBackgroundColor:[UIColor clearColor]];
in your function before return statement
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Because in iOS7 documentation clearly says that default color of each table cell is white. So, if you want to change it then you have to change it from your code. Remember to set the background property to clear color of your tableview from Utilities > Attributes inspector (on your right side).
hope it helps... as it worked for me...!
According to Apple's doc:
Whether you use a predefined or custom cell, you can change the cell’s background using the
backgroundView
property or by changing the inheritedbackgroundColor
property. In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in thetableView:willDisplayCell:forRowAtIndexPath:
method of your table view delegate.
So no matter what color you set in tableView:cellForRowAtIndexPath:
, iOS 7 changes it to white later. Just set it in tableView:willDisplayCell:forRowAtIndexPath:
. Worked for me perfectly.