无法更改iOS 7(iPad)上的静态表格视图单元格的背景颜色

在iOS设备上运行时,我无法更改iOS 7上的静态UITableViewCells的背景颜色。 您可以使用以下设置轻松检查:

  • 在Xcode 5中使用两个故事板创build一个新的通用项目。
  • 在每个故事板中,只放置一个控制器 – 表视图控制器,并将其设置为初始控制器。
  • 在控制器/故事板中的表格视图中放置几个​​(如3个)静态单元格。
  • 在Interface Builder中将每个静态单元格的背景颜色设置为不同的颜色(我使用红色,绿色和清除颜色)。

现在,在iPhone和iPad模拟器(iOS 7)上运行应用程序。

在iPhone模拟器上,一切正常。
而在iPad模拟器上,所有的单元格都是白色的。

我试图通过在Interface Builder中为单元设置运行时属性来强制iPad正常工作:

  • backgroundColor清除颜色
  • contentView.backgroundColor清除颜色
  • backgroundView为零

但没有什么帮助。 实际上,设置contentView.backgroundColor的运行时属性将会改变单元格的颜色,但是它不能用清晰的颜色(这意味着在它后面有另一个白色的视图)。

非常奇怪的是,在同一版本的iOS上的两个设备产生不同的结果。 任何人都可以证实这个错误?

有没有人有这个问题的解决scheme,或者唯一的办法是去dynamic属性+ cellForRowAtIndexPath设置颜色? 我想使用静态单元格,因为问题的性质是静态的。

PS我只是意识到,我忘了尝试设置backgroundView.backgroundColor运行时属性清除颜色,我目前无法访问Mac。 也许这会做的伎俩。

做这个:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { UIImage *pattern = [UIImage imageNamed:@"image.png"]; [cell setBackgroundColor:[UIColor colorWithPatternImage:pattern]]; } 

在IOS7上为我工作

根据苹果的文件 :

无论您使用预定义还是自定义单元格,都可以使用backgroundView属性或通过更改inheritance的backgroundColor属性来更改单元格的背景。 在iOS 7中,默认情况下,单元格具有白色背景; 在早期版本的iOS中,单元格inheritance封闭表视图的背景颜色。 如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作。

因此,无论您在tableView:cellForRowAtIndexPath:设置了什么颜色,iOS 7稍后都会将其更改为白色。 只需在tableView:willDisplayCell:forRowAtIndexPath:设置tableView:willDisplayCell:forRowAtIndexPath: 。 为我完美工作。

我可以解决这个问题的唯一方法是以编程方式而不是使用故事板。 作为参考,我会发布我的解决scheme,我希望它可以帮助任何人。

我replace了uiviewcontroller的uitableviewcontroller。

然后添加这个:

头文件

 #import <UIKit/UIKit.h> @interface LtHomeViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { NSArray *mFiles; NSArray *mPics; } @end 

和模块文件

 #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 

现在我不知道这条线是否会帮助你,但是由于我需要更多的东西,我采取了手动制作表的方法

 - (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]; } 

大卫

有一个更简单的方法来覆盖单元格的背景颜色。 子类UITableViewCell并将其设置为静态tableview单元格的类。 然后覆盖:

 -(void) willMoveToSuperview:(UIView *)newSuperview { self.backgroundColor = [UIColor ...]; } 

其中[UIColor …] =任何你想要的颜色(例如[UIColor clearColor]等);

改变ContentView的背景颜色,它会起作用。

只写这行

[cell setBackgroundColor:[UIColor clearColor]];

在你的函数返回语句之前

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

因为在iOS7文档中清楚地说每个表格单元格的默认颜色是白色的。 所以,如果你想改变它,那么你必须从你的代码中改变它。 请记住设置背景属性清除您的表视图的颜色从实用程序>属性检查器(在右侧)。

希望它可以帮助…因为它为我工作…!