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

发布于 2024-12-01 19:11:51 字数 115 浏览 1 评论 0原文

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

Is there any possibility to highlighting the borders of the selected cell in a UITableViewController in Objective-c?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画离情绘悲伤 2024-12-08 19:11:51

您可以尝试使用 setSelectedBackgroundView: 为选择创建自定义 UIView/UIImageView:

这是我在自定义 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]];

You can try to make an custom UIView/UIImageView for the selection with setSelectedBackgroundView:

Here is a example code I use for custom gradient in a custom 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

else for the whole tableview:

[tableViewController.tableView setSeparatorColor:[UIColor redColor]];
心头的小情儿 2024-12-08 19:11:51

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

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

代码:

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor 红色颜色];
    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."

我在上校的这篇文章中找到了此信息。谢谢他!

This is really simple, since OS 3.0 just set the background color of the cell in the willDisplayCell method. You must not set the color in the 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];
    }

P.S: Here the documentation extract for 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. Thank him!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文