ios UITableView 单元格选择颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1998775/
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
UITableView Cell selected Color?
提问by Momi
I have created a custom UITableViewCell
. The table view is showing data fine. What I am stuck in is when user touches cell of tableview, then I want to show the background color of the cell other than the default [blue color] values for highlighting the selection of cell.
I use this code but nothing happens:
我创建了一个自定义UITableViewCell
. 表视图显示数据很好。我遇到的是当用户触摸 tableview 的单元格时,然后我想显示单元格的背景颜色而不是默认的 [blue color] 值以突出显示单元格的选择。我使用此代码但没有任何反应:
cell.selectedBackgroundView.backgroundColor=[UIColor blackColor];
回答by Maciej Swic
No need for custom cells. If you only want to change the selected color of the cell, you can do this:
无需自定义单元格。如果您只想更改单元格的选定颜色,您可以这样做:
Objective-C:
目标-C:
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
Swift:
迅速:
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.red
cell.selectedBackgroundView = bgColorView
回答by Andrew Little
I think you were on the right track, but according to the class definition for selectedBackgroundView
:
我认为你是在正确的轨道上,但根据类定义selectedBackgroundView
:
The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for section-group tables UITableViewStyleGrouped).
对于普通样式表 (UITableViewStylePlain) 中的单元格,默认值为 nil,对于节组表 UITableViewStyleGrouped,默认值为非 nil。
Therefore, if you're using a plain-style table, then you'll need to alloc-init a new UIView
having your desired background colour and then assign it to selectedBackgroundView
.
因此,如果您使用的是普通样式表,则需要分配初始化一个UIView
具有所需背景颜色的新表,然后将其分配给selectedBackgroundView
.
Alternatively, you could use:
或者,您可以使用:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
if all you wanted was a gray background when the cell is selected. Hope this helps.
如果您想要的只是选中单元格时的灰色背景。希望这可以帮助。
回答by pkamb
回答by Christian Fritz
If you have a grouped table with just one cell per section, just add this extra line to the code:
bgColorView.layer.cornerRadius = 10;
如果您的分组表格每个部分只有一个单元格,只需在代码中添加以下额外行:
bgColorView.layer.cornerRadius = 10;
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
bgColorView.layer.cornerRadius = 10;
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
Don't forget to import QuartzCore.
不要忘记导入 QuartzCore。
回答by phitsch
Swift 3: for me it worked when you put it in the cellForRowAtIndexPath:
method
Swift 3:对我来说,当你把它放在cellForRowAtIndexPath:
方法中时它就起作用了
let view = UIView()
view.backgroundColor = UIColor.red
cell.selectedBackgroundView = view
回答by samwize
The following works for me in iOS 8.
以下在 iOS 8 中对我有用。
I have to set the selection style to UITableViewCellSelectionStyleDefault
for custom background color to work. If any other style, the custom background color will be ignored. There seems to be a change in behaviours as previous answers needs to set style to none instead.
我必须将选择样式设置UITableViewCellSelectionStyleDefault
为自定义背景颜色才能工作。如果有任何其他样式,自定义背景颜色将被忽略。行为似乎发生了变化,因为以前的答案需要将样式设置为无。
The full code for the cell as follows:
单元格的完整代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// This is how you change the background color
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
回答by rajesh
Create a custom cell for your table cell and in the custom cell class.m put the code below, it will work fine. You need to place the desired color image in selectionBackground
UIImage.
为您的表格单元格创建一个自定义单元格,并在自定义单元格 class.m 中放入下面的代码,它会正常工作。您需要在selectionBackground
UIImage 中放置所需的彩色图像。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
UIImage *selectionBackground = [UIImage imageNamed:@"yellow_bar.png"];
UIImageView *iview=[[UIImageView alloc] initWithImage:selectionBackground];
self.selectedBackgroundView=iview;
}
回答by Nik Kov
Swift 3.0 extension
斯威夫特 3.0 扩展
extension UITableViewCell {
var selectionColor: UIColor {
set {
let view = UIView()
view.backgroundColor = newValue
self.selectedBackgroundView = view
}
get {
return self.selectedBackgroundView?.backgroundColor ?? UIColor.clear
}
}
}
cell.selectionColor = UIColor.FormaCar.blue
cell.selectionColor = UIColor.FormaCar.blue
回答by Hemanshu Liya
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];
}
We need to set the selected background view in this method.
我们需要在这个方法中设置选中的背景视图。