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

时间:2011-11-22 10:47:13

标签: iphone ios uitableview

我正在尝试在突出显示时向单元格添加非标准颜色。为此,我创建一个具有我想要的背景颜色的视图,并将其设置为单元格的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'

中添加了这段代码

提前致谢

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

你可以这样做。

您可以为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。

答案 2 :(得分:0)

单元格是否具有固定的大小和高亮区域?

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

答案 3 :(得分:-1)

尝试为selectionView设置帧大小,其中x = 20.我不确定这一点,但我想它应该适用于您的给定场景。

相关问题