繁体   English   中英

选中时突出显示单元格的一部分

[英]highlight a part of the cell when selected

我试图在单元格突出显示时向其添加非标准颜色。 为此,我用所需的背景色创建了一个视图,并将其设置为单元格的selectedBackgroundView

一切都很好。

 UIView *selectionView = [[UIView alloc] init]; [selectionView setBackgroundColor:[UIColor colorWithRed:(121/255.0) green:(201/255.0) blue:(209/255.0) alpha:1.0]]; [cell setSelectedBackgroundView:selectionView]; 

我的问题是,我可以更改selectedBackgroundView的框架,以便仅突出显示单元格的一部分吗(确切地说,我希望selectionBackroundView的X偏移为20像素)。

有什么简单的方法吗?

更新的代码:

 UIView *selectionView = [[UIView alloc] init]; [selectionView setBackgroundColor:[UIColor clearColor]]; 
UIView *selectionSubView = [[UIView alloc] initWithFrame:(CGRectMake(20.0f, 0.0f, 300.0f, 72.0f))];
[selectionSubView setBackgroundColor:[UIColor colorWithRed:(121/255.0) green:(201/255.0) blue:(209/255.0) alpha:1.0]];

UIView *clearView = [[UIView alloc] initWithFrame:(CGRectMake(0.0f, 0.0f, 20.0f, 72.0f))];
[clearView setBackgroundColor: [UIColor clearColor]];

[selectionView addSubview: selectionSubView];
[selectionView addSubview: clearView];

[cell setSelectedBackgroundView: selectionView];

这似乎也不起作用。 我已经在“ cellForRowAtIndexPath”中添加了此代码

提前致谢

您可以将较小的UIView作为selectionView的子视图,并更改该视图的背景颜色。

你可以这样

您为UIView创建单独的文件,如下所示。

TestView.m

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
    // Initialization code.
    [self setBackgroundColor:[UIColor clearColor]];
}
return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

/* Draw a circle */
// Get the contextRef
CGContextRef contextRef = UIGraphicsGetCurrentContext();

// Set the border width
CGContextSetLineWidth(contextRef, 1.0);

// Set the circle fill color to GREEN
CGContextSetRGBFillColor(contextRef, 100.0, 255.0, 0.0, 1.0);

// Set the cicle border color to BLUE
CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 255.0, 1.0);

// Fill the circle with the fill color
CGContextFillRect(contextRef, CGRectMake(20, 0, rect.size.width, rect.size.height));

// Draw the circle border
//CGContextStrokeRectWithWidth(contextRef, rect, 10);//(contextRef, rect);
}

这个自定义视图可以用作像这样的单元格选择的背景视图。

    TestView *bgView = [[TestView alloc] initWithFrame:cell.frame]; // Creating a view for the background...this seems to be required.
cell.selectedBackgroundView = bgView;

可能对您有帮助。

谢谢,

Minesh Purohit。

单元格的大小和突出显示区域是否固定?

如果是,则创建图像并将图像视图用作selectedBackgroundView。

尝试为x = 20的selectionView设置帧大小。我不确定这一点,但我认为它应该适用于给定的方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM