2011-08-26 30 views

回答

2

你可以嘗試做一個自定義的UIView /的UIImageView的選擇與setSelectedBackgroundView:

這裏是一個示例代碼我在自定義tableviewcell使用自定義的梯度:

UIView *selctionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)] autorelease]; 

CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame = selctionView.bounds; 
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor], (id)[[UIColor redColor] CGColor], nil]; 

[selctionView.layer insertSublayer:gradient atIndex:0]; 

[self setSelectedBackgroundView:selctionView]; 

編輯:

我發現你也可以使用下面的方法:

[test.layer setBorderColor: [[UIColor redColor] CGColor]]; 
[test.layer setBorderWidth: 1.0]; 

爲圖層。

一定要導入QuartzCore.h

否則整個實現代碼如下:

[tableViewController.tableView setSeparatorColor:[UIColor redColor]]; 
+0

嘿謝謝。這是我的實際要求:我使用的是一個splitviewcontroller。在這裏,當我在rootview控制器中選擇特定的單元格時,需要更改相應單元格的4個邊框顏色。 – Tinku

+0

沒問題,你也可以選擇一個動畫(例如將漸變從「黑色變爲淺色」變爲「光變爲黑色」),看起來非常棒,我只在選擇時再次選擇一些缺陷。 – Justin

+0

是的,那真的很好。如何更改選定的單元格textcolor而不影響其他單元格? – Tinku

0

這是非常簡單的,因爲OS 3.0剛剛成立的willDisplayCell方法的單元格的背景顏色。您不能在cellForRowAtIndexPath中設置顏色。

這同時適用於平原和分組方式:

代碼:

  • (無效)的tableView:(UITableView的*)的tableView willDisplayCell:(的UITableViewCell *)細胞forRowAtIndexPath:(NSIndexPath *)indexPath cell.backgroundColor = [UIColor redColor]; }

P.S:這裏willDisplayCell文件摘錄:

"A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out." 

我發現這個職位從colionel此信息。謝謝他!