2011-11-22 83 views
0

我試圖在突出顯示的單元格中添加非標準顏色。爲此,我使用我想要的背景顏色創建視圖,並將其設置爲單元格的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」

在此先感謝

+0

有點匆忙,現在無法編譯它,但你有沒有嘗試過明確設置selectionView框架? '因爲如果添加子視圖時沒有特定的框架,結果可能不正確(子視圖座標相對於父項,但如果父項沒有...)。 –

回答

1

添加該代碼你可以把一個小的UIView作爲您selectionView的子視圖,並更改視圖的THA背景顏色。

+0

你可以檢查我的更新嗎? – Neelesh

+0

它不工作。我試過那個。 – Neelesh

-1

嘗試爲selectionView設置框架大小,其中x = 20。我不確定這個,但我想它應該適用於您的給定場景。

0

單元格是否具有固定大小和高亮區域?

如果是,創建一個圖像,並使用圖像視圖作爲selectedBackgroundView

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。

+0

不錯的一個....... – Maulik

相關問題