xcode 无法更改 UITableViewCell 背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9118902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can't change UITableViewCell background color
提问by Yossi Tsafar
I have UITableViewCell
that fits on UITableView
, but somehow I can't change the background color when entering editing style mode.. I've tried everything on the web, search all day long, but still couldn't get it fixed (I've searched StackOverflow as-well). please, help me.
我有UITableViewCell
合适的UITableView
,但不知何故我无法在进入编辑样式模式时更改背景颜色..我已经尝试了网络上的所有内容,搜索了一整天,但仍然无法修复它(我已经搜索过StackOverflow 也是)。请帮我。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (MainTableViewCell *)view;
}
}
}
回答by Alex Stanciu
Try customizing the cell inside tableView: willDisplayCell:
method, something like this:
尝试自定义 cell insidetableView: willDisplayCell:
方法,如下所示:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
回答by Westley
You need to change the tableView cell's contentView, not the cell's background view.
您需要更改 tableView 单元格的 contentView,而不是单元格的背景视图。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.contentView.backgroundColor = [UIColor redColor];
}
回答by manuelBetancurt
to have total control on the customisation of your cells, I prefer better to override the cell view,
要完全控制单元格的自定义,我更喜欢覆盖单元格视图,
create a new class for your cell view, that gets called by the UITableView,
为您的单元格视图创建一个新类,由 UITableView 调用,
later when i get to my work computer if you havent found your answer I will post some sample code,
稍后当我到达我的工作计算机时,如果您还没有找到答案,我将发布一些示例代码,
once you see how it works is pretty easy
一旦你看到它是如何工作的就很容易了
you could as well place an image for your cell background, and place different labels and images, buttons, textfields in custom places of your cell,
您也可以为单元格背景放置一个图像,并在单元格的自定义位置放置不同的标签和图像、按钮、文本字段,
EDIT>>
编辑>>
the code! [is overkill just to change the background, but if you want to really customise your cell, this is the way to go!]
编码![只是改变背景是矫枉过正的,但如果你想真正自定义你的单元格,这是要走的路!]
in your CustomCell.h
在您的 CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
UILabel *_kLabel;
UILabel *_dLabel;
}
@property (nonatomic, retain) UILabel *kLabel;
@property (nonatomic, retain) UILabel *dLabel;
- (void) initLabels;
@end
in your CustomCell.m
在您的 CustomCell.m 中
#import "CustomCell.h"
@implementation CustomCell
@synthesize kLabel = _kLabel;
@synthesize dLabel = _dLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
CGRect popUpImageBgndRect = CGRectMake(0, 0, 942, 44);
UIImageView *popUpImageBgnd = [[UIImageView alloc] initWithFrame:popUpImageBgndRect];
[popUpImageBgnd setImage:[UIImage imageNamed:@"tableCellBgnd.png"]];
popUpImageBgnd.opaque = YES; // explicitly opaque for performance
[self.contentView addSubview:popUpImageBgnd];
[popUpImageBgnd release];
[self initLabels];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,10, 200, 20);
self.kLabel.frame = frame;
frame= CGRectMake(boundsX+98 ,10, 100, 20);
self.dLabel.frame = frame;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void) initLabels {
self.kLabel = [[[UILabel alloc]init] autorelease];
self.kLabel.textAlignment = UITextAlignmentLeft;
self.kLabel.backgroundColor = [UIColor clearColor];
self.kLabel.font = [UIFont fontWithName:@"FS Albert" size:16];
self.kLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
self.dLabel = [[[UILabel alloc]init] autorelease];
self.dLabel.textAlignment = UITextAlignmentLeft;
self.dLabel.backgroundColor = [UIColor clearColor];
self.dLabel.font = [UIFont systemFontOfSize:16];
self.dLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
[self.contentView addSubview:self.kLabel];
[self.contentView addSubview:self.dLabel];
}
-(void) dealloc {
[_kLabel release];
[_dLabel release];
[super dealloc];
}
@end
And in your ViewController.m
在你的 ViewController.m
YourViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
ENJOY!! ;)
请享用!!;)
回答by Paulo Miguel Almeida
As @Westley mentioned;
正如@Westley 提到的;
You need to change the tableView cell's contentView, not the cell's background view.
您需要更改 tableView 单元格的 contentView,而不是单元格的背景视图。
This is how you should do it:
你应该这样做:
if(index == conditionYouWant)
{
cell.contentView.backgroundColor = [UIColor whiteColor];
}
else
{
cell.contentView.backgroundColor = [UIColor colorWithRed:0.925 green:0.933 blue:0.937 alpha:1.0];
}
Hope it helps you guys out
希望能帮到大家
回答by dev4life
Be sure you didn't already set the content view's background color in the Storyboard. If you did, changing the cell.backgroundColor in code will make no difference, and you will go round and round in confusion (like I did).
确保您尚未在 Storyboard 中设置内容视图的背景颜色。如果你这样做了,更改代码中的 cell.backgroundColor 将没有任何区别,并且你会在混乱中绕来绕去(就像我所做的那样)。
回答by Deep
cell.backgroundColor = [UIColor redColor];
回答by Deep
in swift 3 you can use
在 swift 3 你可以使用
cell.backgroundcolor = UIColor.white
回答by Daniel
try setting the cells backgroundColor property, worked for me cell.backgroundColor=[UIColor blueColor];
尝试设置单元格 backgroundColor 属性,对我有用 cell.backgroundColor=[UIColor blueColor];
回答by hieund
Please try this, it works for me.
请试试这个,它对我有用。
UIView *bgView = [[UIView alloc] initWithFrame:cell.frame];
bgView.backgroundColor = [UIColor greenColor];
cell.backgroundView = bgView;
[bgView release];