繁体   English   中英

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

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

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

您可以尝试使用setSelectedBackgroundView:为选择创建自定义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]];

这非常简单,因为OS 3.0只是在willDisplayCell方法中设置单元格的背景颜色。 您不能在cellForRowAtIndexPath中设置颜色。

这适用于普通和分组样式:

码:

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

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."

我在colionel的这篇文章中找到了这些信息。 谢谢他!

暂无
暂无

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

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