简体   繁体   English

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

[英]Unable to change background color of static table view cell on iOS 7 (iPad)

I am unable to change the background color of static UITableViewCells on iOS 7, when running on iPad device. 在iPad设备上运行时,我无法在iOS 7上更改静态UITableViewCells的背景颜色。 You can easily check this with following setup: 您可以使用以下设置轻松检查:

  • Make a new universal project in Xcode 5 with two storyboards. 使用两个故事板在Xcode 5中创建一个新的通用项目。
  • In each storyboard, put only one controller - table view controller, and set it as the initial one. 在每个故事板中,只放置一个控制器 - 表视图控制器,并将其设置为初始控制器。
  • Put a few (eg 3) static cells in the table view in both controllers/storyboards. 在两个控制器/故事板中的表视图中放置一些(例如3个)静态单元。
  • Set the background color of each static cell to different colors in Interface Builder (I use red, green, and clear colors). 在Interface Builder中将每个静态单元格的背景颜色设置为不同的颜色(我使用红色,绿色和清晰的颜色)。

Now, run the app on iPhone and iPad simulators (iOS 7). 现在,在iPhone和iPad模拟器(iOS 7)上运行该应用程序。

On the iPhone simulator, everything is ok; 在iPhone模拟器上,一切都很好;
while on the iPad simulator, all cells are colored white. 在iPad模拟器上,所有细胞都是白色的。

I tried to force iPad to work correctly by setting runtime properties in Interface Builder for cells: 我试图通过在Interface Builder中为单元格设置运行时属性来强制iPad正常工作:

  • backgroundColor to clear color backgroundColor清除颜色
  • contentView.backgroundColor to clear color contentView.backgroundColor清除颜色
  • backgroundView to nil backgroundView为零

but nothing helps. 但没有任何帮助。 Actually, setting the runtime property of contentView.backgroundColor will change the cell color, but it does not work with clear color (which means there is another view colored in white behind it). 实际上,设置contentView.backgroundColor的运行时属性将改变单元格颜色,但它不能用于清晰的颜色(这意味着在其后面有另一个白色的视图)。

It is very strange that two devices on same version of iOS produce different results. 相同版本的iOS上的两个设备产生不同的结果是非常奇怪的。 Can anyone else confirm this bug? 其他人可以确认这个bug吗?

Does anyone have a solution to this problem, or the only way is to go for dynamic properties + setting color in cellForRowAtIndexPath ? 有没有人有这个问题的解决方案,或唯一的方法是在cellForRowAtIndexPath寻找动态属性+设置颜色? I would like to use static cells, because the nature of the problem is static. 我想使用静态单元格,因为问题的本质是静态的。

ps I just realized that I forgot to try to set the backgroundView.backgroundColor runtime property to clear color, and I don't have access to a Mac at the moment. ps我刚刚意识到我忘了尝试将backgroundView.backgroundColor运行时属性设置为清除颜色,而目前我无法访问Mac。 Maybe that would do the trick. 也许这会成功。

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 在IOS7上为我工作

According to Apple's doc : 根据Apple的文档

Whether you use a predefined or custom cell, you can change the cell's background using the backgroundView property or by changing the inherited backgroundColor property. 无论使用预定义单元格还是自定义单元格,都可以使用backgroundView属性或更改继承的backgroundColor属性来更改单元格的背景。 In iOS 7, cells have a white background by default; 在iOS 7中,单元格默认为白色背景; in earlier versions of iOS, cells inherit the background color of the enclosing table view. 在早期版本的iOS中,单元格继承封闭表视图的背景颜色。 If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate. 如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作。

So no matter what color you set in tableView:cellForRowAtIndexPath: , iOS 7 changes it to white later. 因此,无论您在tableView:cellForRowAtIndexPath:设置什么颜色,iOS 7都会在以后将其更改为白色。 Just set it in tableView:willDisplayCell:forRowAtIndexPath: . 只需在tableView:willDisplayCell:forRowAtIndexPath:设置它tableView:willDisplayCell:forRowAtIndexPath: . Worked for me perfectly. 完美地为我工作。

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. 我将uitableviewcontroller替换为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 大卫

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. 子类UITableViewCell并将其设置为静态tableview单元的类。 Then override: 然后覆盖:

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

where [UIColor ...] = whatever color you want (eg [UIColor clearColor] etc); 其中[UIColor ...] =你想要的任何颜色(例如[UIColor clearColor]等);

改为改变ContentView背景颜色,它将起作用。

just write this line 写下这一行

[cell setBackgroundColor:[UIColor clearColor]]; [cell setBackgroundColor:[UIColor clearColor]];

in your function before return statement 在return函数之前的函数中

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

Because in iOS7 documentation clearly says that default color of each table cell is white. 因为在iOS7文档中明确说明每个表格单元格的默认颜色是白色。 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...! 希望它有所帮助......因为它对我有用......!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM