Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 关于如何将UIView屏蔽为UITableViewCell Selected BackgroundView的明确答案_Ios_Uiview_Uitableview - Fatal编程技术网

Ios 关于如何将UIView屏蔽为UITableViewCell Selected BackgroundView的明确答案

Ios 关于如何将UIView屏蔽为UITableViewCell Selected BackgroundView的明确答案,ios,uiview,uitableview,Ios,Uiview,Uitableview,我已经阅读并尝试了在StackOverflow上找到的一些答案。我也从博客上读过并尝试过一些东西,但似乎什么也做不到 我创建了一个UIView,并将其背景色设置为所需的UITableViewCell选择颜色(而不是标准的蓝色或灰色选择颜色)。我将此ui视图添加到我的单元格的selectedBackgroundView中,效果很好,我的单元格在用户选择时会更改为所需的颜色 此方法在普通uitableview上非常有效;不太适合分组。在分组的UITableView上,第一个和最后一个单元格不符合剪辑

我已经阅读并尝试了在StackOverflow上找到的一些答案。我也从博客上读过并尝试过一些东西,但似乎什么也做不到

我创建了一个
UIView
,并将其背景色设置为所需的
UITableViewCell
选择颜色(而不是标准的蓝色或灰色选择颜色)。我将此
ui视图
添加到我的单元格的
selectedBackgroundView
中,效果很好,我的单元格在用户选择时会更改为所需的颜色

此方法在普通
uitableview
上非常有效;不太适合分组。在分组的
UITableView
上,第一个和最后一个单元格不符合剪辑/遮罩边界,如下面的屏幕截图所示

我知道没有办法只绕过左上角和右上角

我想严格按照代码来做,没有图像

问题: 有人知道有什么好办法可以改变
uitableview单元格的
selectedBackgroundView
颜色,只使用
UIView
而不使用图像,并使第一个和最后一个单元格符合圆角边界吗?

例子 截图

解决方案 我接受了,因为他添加了一条评论,指出了一个非常好(但很长)的解决方案。我不得不做了相当多的修改,但最后,我现在有了我需要的selectedBackgroundView,它在第一个和最后一个单元格的拐角处,再次感谢


由于单元格的复杂性,我假设您使用的是UITableViewCell子类。我就是这样做的:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
    {
        self.clipsToBounds = YES;

        UIView* bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.25f];
        self.selectedBackgroundView = bgView;
                //other code

    }
    return self;
}
这会在单元格上生成一种深灰色覆盖,而不需要图像

在您的情况下,您选择的单元格的确切颜色(多亏了方便的dandy数字色度计)为

[UIColor colorWithRed:106.0f/255.0f green:51.0f/255.0f blue:6.0f/255.0f alpha:1.0f];
而白色文本将是

- (void)setSelected:(BOOL)sel animated:(BOOL)animated
{
    [super setSelected:sel animated:animated];

    if (sel)
    {
        self.textLabel.textColor = [UIColor whiteColor];

        }
    else
    {
        self.textLabel.textColor = [UIColor colorWithRed:(105.f/255.f) green:(50.f/255.f) blue:(6.f/255.f) alpha:1.f];  

    }
}

我也遇到了同样的问题,通过在UITableViewCell子类中重写
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
,并在创建单元格时设置无选择样式,解决了这个问题

//set no selection style in the cell
...
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
...

//override setHighlighted to manually set the regular/highlighted background colors
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if(highlighted){
        self.backgroundColor = [UIColor greenColor];
    }
    else {
        self.backgroundColor = [UIColor redColor];
    }
}

这个答案似乎有点用,我想我可以解决这个问题。我对该链接中的代码唯一的一个问题是,选择认为它总是在第一个单元格上,所以掩码总是“第一个单元格掩码”。我将其标记为“第一个单元格掩码”。因为有很多人对此网站不太了解,他们也有同样的问题。但你是对的,谢谢。
//set no selection style in the cell
...
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
...

//override setHighlighted to manually set the regular/highlighted background colors
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if(highlighted){
        self.backgroundColor = [UIColor greenColor];
    }
    else {
        self.backgroundColor = [UIColor redColor];
    }
}