UITableViewCell:选择时附件视图消失

发布于 2024-11-16 19:40:20 字数 606 浏览 3 评论 0原文

我可能会做错所有事情,所以希望有人能告诉我应该做什么。

我构建了一个表格视图来充当图例和颜色选择器。在 AccessoryView 插槽中,我放置了一个自定义视图,它只是一个彩色框。用户将显示图例,这样他们就知道颜色的含义,并且可以选择一种颜色,然后通过将该颜色分配给对象来为绘图“着色”。所有这些都运行得很好。

我遇到的问题是所选的行样式。当我从图例中选择一行时,单元格会变成蓝色,就像它应该的那样,但我的 AccessoryView 现在已经消失了。我不希望这种事发生。但是,我不知道如何设置才能使其不消失。请记住,我仍然希望一行显示为“选定”。但无论我这样做,我的附件视图都会消失(很可能被所选颜色隐藏)。

这是我现在设置附件视图的方法。

CGRect colorBox = CGRectMake(0, 0, 30, 30);
UIView *colorView = [[UIView alloc] initWithFrame:colorBox];
colorView.backgroundColor = [self colorWithHexString:[selOption valueForKey:@"qoColor"]];
cell.accessoryView = colorView;

I might be going about this all wrong, so hopefully someone will tell me what I should be doing.

I have constructed a table view to act as a legend and color picker. In the AccessoryView slot, I places a custom view that is just a colored box. User will show the legend, so they know what colors mean what, and they can pick on a color to then 'colorize' a drawing by assigning that color to objects. All of that works just fine.

What I'm having a problem with is the selected row style. When I select a row from the legend, the cell goes blue, like it should, but my AccessoryView has now disappeared. I don't want this to happen. However, I don't know what to set to make it not disappear. Keep in mind, I still want a row to show up as 'selected'. But any way that I do that, my accessory view disappears (most likely is hidden by the selected color).

Here's how I'm setting the accessory view right now.

CGRect colorBox = CGRectMake(0, 0, 30, 30);
UIView *colorView = [[UIView alloc] initWithFrame:colorBox];
colorView.backgroundColor = [self colorWithHexString:[selOption valueForKey:@"qoColor"]];
cell.accessoryView = colorView;

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

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

发布评论

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

评论(1

⒈起吃苦の倖褔 2024-11-23 19:40:20

您可以使用 UIImageView 代替 UIView,选择单元格时 UIView 不会消失。您可以为每种颜色制作微小的 .png 缩略图(如果没有很多),也可以在 cellForRowAtIndexPath 委托方法中动态创建它们,ala:

UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[self colorWithHexString:[selOption valueForKey:@"qoColor"]]; CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[colorView setImage:image];

[cell setAccessoryView:colorView];
[colorView release];

You can use a UIImageView instead of a UIView, which won't disappear when the cell is selected. You can either make tiny .png thumbnails for each color (if there aren't a lot) or you can create them dynamically in your cellForRowAtIndexPath delegate method, ala:

UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[self colorWithHexString:[selOption valueForKey:@"qoColor"]]; CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[colorView setImage:image];

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