在iPad设备上运行时,我无法更改iOS 7上静态UITableViewCells的背景颜色。您可以使用以下设置轻松检查:
现在,在iPhone和iPad模拟器(iOS 7)上运行该应用程序。
在iPhone模拟器上,一切正常;
在iPad模拟器上,所有细胞都是白色的。
我尝试通过在Interface Builder中为单元格设置运行时属性来强制iPad正常工作:
backgroundColor
清除颜色contentView.backgroundColor
清除颜色backgroundView
到nil 但没有任何帮助。实际上,设置contentView.backgroundColor
的运行时属性将改变单元格颜色,但它不能用于清晰的颜色(这意味着在其后面有另一个白色的视图)。
相同版本的iOS上的两个设备产生不同的结果是非常奇怪的。其他人可以确认这个错误吗?
有没有人能解决这个问题,或者唯一的方法是在cellForRowAtIndexPath
中选择动态属性+设置颜色?我想使用静态单元格,因为问题的本质是静态的。
P.S。我刚刚意识到我忘了尝试将backgroundView.backgroundColor
运行时属性设置为清除颜色,而我目前无法访问Mac。也许那就行了。
答案 0 :(得分:57)
这样做:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *pattern = [UIImage imageNamed:@"image.png"];
[cell setBackgroundColor:[UIColor colorWithPatternImage:pattern]];
}
在IOS7为我工作
答案 1 :(得分:10)
根据Apple's doc:
无论是使用预定义单元格还是自定义单元格,都可以使用
backgroundView
属性或更改继承的backgroundColor
属性来更改单元格的背景。在iOS 7中,单元格默认为白色背景;在早期版本的iOS中,单元格继承封闭表视图的背景颜色。如果要更改单元格的背景颜色,请在表格视图委托的tableView:willDisplayCell:forRowAtIndexPath:
方法中执行此操作。
因此,无论您在tableView:cellForRowAtIndexPath:
中设置什么颜色,iOS 7都会在以后将其更改为白色。只需在tableView:willDisplayCell:forRowAtIndexPath:
中设置即可。完美地为我工作。
答案 2 :(得分:1)
解决此问题的唯一方法是以编程方式创建表而不是使用故事板。作为参考,我将发布我的解决方案,我希望它可以帮助任何人。
我将uitableviewcontroller替换为uiviewcontroller。
然后添加了这个:
头文件
#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];
}
大卫
答案 3 :(得分:1)
有一种更简单的方法来覆盖单元格背景颜色。子类UITableViewCell并将其设置为静态tableview单元的类。然后覆盖:
-(void) willMoveToSuperview:(UIView *)newSuperview {
self.backgroundColor = [UIColor ...];
}
其中[UIColor ...] =您想要的任何颜色(例如[UIColor clearColor]等);
答案 4 :(得分:1)
改为改变ContentView背景颜色,它将起作用。
答案 5 :(得分:0)
只需写下这一行
在返回语句之前的函数中[cell setBackgroundColor:[UIColor clearColor]];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
因为在iOS7文档中明确指出每个表格单元格的默认颜色是白色。因此,如果您想要更改它,则必须从代码中进行更改。 请记住将背景属性设置为从Utilities&gt;中清除tableview的颜色。属性检查员(在您的右侧)。
希望它有所帮助......因为它对我有用......!