Sil*_*ail 8 objective-c uitableview
我环顾四周找到了一个解决方案,用于将accessoryView的背景颜色设置为与单元格的contentView相同的背景颜色.
cell.contentView.backgroundColor = [UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];
cell.accessoryView.backgroundColor =[UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];
Run Code Online (Sandbox Code Playgroud)
有一个解决方案可行但只允许我为所有单元格使用一种颜色.
cell.contentView.superView.backgroundColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)
唯一的解决方案是不使用accessoryView并使用图像代替?
谢谢!
Kel*_*ler 28
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor greenColor];
}
Run Code Online (Sandbox Code Playgroud)
使用此UITableViewDelegate方法,您可以将单元格的颜色设置为不同的颜色.请注意,Apple明确建议您在文档中backgroundColor的tableView:willDisplayCell:ForRowAtIndexPath:方法中对属性进行更改,其中声明:
如果要更改单元格的背景颜色,请在tableView:willDisplayCell:forRowAtIndexPath:表视图委托方法中执行此操作
实际上,在iOS 6中,从其他任何地方(如tableView:cellForRowAtIndexPath:方法)对属性的更改根本不会产生任何影响.在iOS 7中似乎不再是这种情况,但Apple建议从内部修改属性tableView:willDisplayCell:ForRowAtIndexPath:(没有任何解释).
对于交替颜色,请执行以下示例:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2) {
cell.backgroundColor = [UIColor yellowColor];
} else {
cell.backgroundColor = [UIColor redColor];
}
}
Run Code Online (Sandbox Code Playgroud)
小智 18
我也挣扎了一会儿,并使用配件创建了一个自定义图像.但我发现这个解决方案运行良好,不需要自定义图像.诀窍是更改单元格的backgroundView颜色而不是backgroundColor.
UIView *myView = [[UIView alloc] init];
if (indexPath.row % 2) {
myView.backgroundColor = [UIColor whiteColor];
} else {
myView.backgroundColor = [UIColor blackColor];
}
cell.backgroundView = myView;
Run Code Online (Sandbox Code Playgroud)
无需更改accessoryView或contentView背景颜色.他们会自动关注.
请注意2014年.通常你会使用 - (void)setSelected:(BOOL)选择动画:(BOOL)动画
所以,你有一个自定义的单元类,你可以像这样设置正常/选择的颜色......
HappyCell.h
@interface HappyCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *mainLabel;
etc...
@end
HappyCell.m
@implementation HappyCell
-(id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
}
return self;
}
-(void)awakeFromNib
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
self.backgroundColor = [UIColor redColor];
.. other setup for selected cell
}
else
{
self.backgroundColor = [UIColor yellowColor];
.. other setup for normal unselected cell
}
}
@end
// to help beginners.......
// in your table view class, you'd be doing this...
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return yourDataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tv
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger thisRow = indexPath.row;
ContentsCell *cell = [tv
dequeueReusableCellWithIdentifier:@"cellName"
forIndexPath:indexPath];
// "cellName" must be typed in on the cell, the storyboard
// it's the "identifier", NOT NOT NOT the restorationID
[cell setupForNumber: thisRow];
cell.mainLabel.text = yourDataArray[ thisRow ][@"whatever"];
cell.otherLabel.text = yourDataArray[ thisRow ][@"whatever"];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
希望它可以帮到某人.
这对我有用:
cell.contentView.backgroundColor = [UIColor darkGreyColor];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22231 次 |
| 最近记录: |