Mom*_*omi 310 objective-c uitableview ios selection-color
我创建了一个自定义UITableViewCell
.表格视图显示数据正常.我所困扰的是当用户触摸tableview的单元格时,我想显示除默认[蓝色]值以外的单元格的背景颜色,以突出显示单元格的选择.我使用这段代码但没有任何反应:
cell.selectedBackgroundView.backgroundColor=[UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)
Mac*_*wic 648
无需自定义单元格.如果您只想更改单元格的选定颜色,则可以执行以下操作:
Objective-C的:
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
Run Code Online (Sandbox Code Playgroud)
迅速:
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.red
cell.selectedBackgroundView = bgColorView
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
Run Code Online (Sandbox Code Playgroud)
编辑:已更新为ARC
编辑:添加Swift 3
And*_*tle 359
我认为你是在正确的轨道上,但根据类定义selectedBackgroundView
:
对于普通样式表(UITableViewStylePlain)中的单元格,默认值为nil;对于节组表UITableViewStyleGrouped,默认值为nil.
因此,如果您使用的是普通样式的表,那么您需要UIView
使用所需的背景颜色alloc-init一个新的,然后将其分配给selectedBackgroundView
.
或者,您可以使用:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
Run Code Online (Sandbox Code Playgroud)
如果你想要的只是在选择单元格时的灰色背景.希望这可以帮助.
小智 37
如果您有一个分组表,每个部分只有一个单元格,只需将此额外行添加到代码中:
bgColorView.layer.cornerRadius = 10;
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
bgColorView.layer.cornerRadius = 10;
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
Run Code Online (Sandbox Code Playgroud)
别忘了导入QuartzCore.
phi*_*sch 28
Swift 3:对我而言,当你把它放在cellForRowAtIndexPath:
方法中时它就起作用了
let view = UIView()
view.backgroundColor = UIColor.red
cell.selectedBackgroundView = view
Run Code Online (Sandbox Code Playgroud)
sam*_*ize 22
以下适用于iOS 8.
我必须将选择样式设置UITableViewCellSelectionStyleDefault
为自定义背景颜色才能工作.如果是任何其他样式,将忽略自定义背景颜色.行为似乎有变化,因为之前的答案需要将样式设置为无.
单元格的完整代码如下:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
小智 18
为您的表格单元格创建一个自定义单元格,并在自定义单元格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;
}
Run Code Online (Sandbox Code Playgroud)
Nik*_*Kov 11
extension UITableViewCell {
var selectionColor: UIColor {
set {
let view = UIView()
view.backgroundColor = newValue
self.selectedBackgroundView = view
}
get {
return self.selectedBackgroundView?.backgroundColor ?? UIColor.clear
}
}
}
Run Code Online (Sandbox Code Playgroud)
cell.selectionColor = UIColor.FormaCar.blue
小智 9
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];
}
Run Code Online (Sandbox Code Playgroud)
我们需要在此方法中设置选定的背景视图.
在表格单元格中添加以下行
let bgColorView = UIView()
bgColorView.backgroundColor = .red
self.selectedBackgroundView = bgColorView
Run Code Online (Sandbox Code Playgroud)
最后应该如下所示
override func setSelected(_ selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
let bgColorView = UIView()
bgColorView.backgroundColor = .red
self.selectedBackgroundView = bgColorView
}
Run Code Online (Sandbox Code Playgroud)
如果你想为你的单元格添加一个自定义突出显示的颜色(你的单元格包含按钮,标签,图像等).我按照下面的步骤操作:
例如,如果您想要一个选定的黄色:
1)创建一个适合所有具有20%不透明度(黄色)的单元格的视图,例如backgroundselectedView
2)在单元控制器中写这个:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.backgroundselectedView.alpha=1;
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.backgroundselectedView.alpha=0;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
self.backgroundSelectedImage.alpha=0;
[super touchesCancelled:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
在Swift 4中,您还可以全局设置表格单元格的背景颜色(取自此处):
let backgroundColorView = UIView()
backgroundColorView.backgroundColor = UIColor.red
UITableViewCell.appearance().selectedBackgroundView = backgroundColorView
Run Code Online (Sandbox Code Playgroud)
如果您使用自定义TableViewCell,您还可以覆盖awakeFromNib
:
override func awakeFromNib() {
super.awakeFromNib()
// Set background color
let view = UIView()
view.backgroundColor = UIColor.redColor()
selectedBackgroundView = view
}
Run Code Online (Sandbox Code Playgroud)
我想指出,XIB 编辑器为您提供以下标准选项:
部分:蓝色/灰色/无
(带有选项的右侧列,第 4 个选项卡,第一组“表格视图单元格”,第 4 个子组,3 个项目中的第一个为“选择”)
选择正确的标准选项可能可以实现您想要做的事情。
还有一个提示基督徒的方式来显示分组表的圆角背景.
如果我cornerRadius = 10
用于单元格,它会显示四个角落的圆形选择背景.它与表视图的默认UI不同.
所以,我想到了使用cornerRadius解决它的简单方法.从下面的代码中可以看到,检查单元格的位置(顶部,底部,中间或顶部底部),并添加一个子图层以隐藏顶角或底角.这只是显示与默认表格视图的选择背景完全相同的外观.
我用iPad测试了这段代码splitterview
.您可以根据需要更改patchLayer的帧位置.
如果有更简单的方法可以获得相同的结果,请告诉我.
if (tableView.style == UITableViewStyleGrouped)
{
if (indexPath.row == 0)
{
cellPosition = CellGroupPositionAtTop;
}
else
{
cellPosition = CellGroupPositionAtMiddle;
}
NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
if (indexPath.row == numberOfRows - 1)
{
if (cellPosition == CellGroupPositionAtTop)
{
cellPosition = CellGroupPositionAtTopAndBottom;
}
else
{
cellPosition = CellGroupPositionAtBottom;
}
}
if (cellPosition != CellGroupPositionAtMiddle)
{
bgColorView.layer.cornerRadius = 10;
CALayer *patchLayer;
if (cellPosition == CellGroupPositionAtTop)
{
patchLayer = [CALayer layer];
patchLayer.frame = CGRectMake(0, 10, 302, 35);
patchLayer.backgroundColor = YOUR_BACKGROUND_COLOR;
[bgColorView.layer addSublayer:patchLayer];
}
else if (cellPosition == CellGroupPositionAtBottom)
{
patchLayer = [CALayer layer];
patchLayer.frame = CGRectMake(0, 0, 302, 35);
patchLayer.backgroundColor = YOUR_BACKGROUND_COLOR;
[bgColorView.layer addSublayer:patchLayer];
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
310036 次 |
最近记录: |