简体   繁体   English

在UITableView中突出显示所选单元格的边框

[英]Highlight the borders of the selected cell in UITableView

是否有可能在Objective-c中的UITableViewController突出显示 所选单元格边框

You can try to make an custom UIView/UIImageView for the selection with setSelectedBackgroundView: 您可以尝试使用setSelectedBackgroundView:为选择创建自定义UIView / UIImageView setSelectedBackgroundView:

Here is a example code I use for custom gradient in a custom tableviewcell: 这是我在自定义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];

EDIT: 编辑:

I found out that you also can use the methods: 我发现你也可以使用这些方法:

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

for the layer. 对于图层。

be sure to import QuartzCore.h 一定要导入QuartzCore.h

else for the whole tableview: 整个桌面视图的其他内容:

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

This is really simple, since OS 3.0 just set the background color of the cell in the willDisplayCell method. 这非常简单,因为OS 3.0只是在willDisplayCell方法中设置单元格的背景颜色。 You must not set the color in the cellForRowAtIndexPath. 您不能在cellForRowAtIndexPath中设置颜色。

This works for both the plain and grouped style : 这适用于普通和分组样式:

Code: 码:

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [UIColor redColor]; (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {cell.backgroundColor = [UIColor redColor]; } }

PS: Here the documentation extract for willDisplayCell : PS:这里是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."

I've found this information in this post from colionel. 我在colionel的这篇文章中找到了这些信息。 Thank him! 谢谢他!

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

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