2011-08-26 58 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此信息。谢谢他!