UITableViewCellSeparatorStyleNone 在 UITableView 中选择时不会隐藏蓝色分隔线

发布于 2024-08-31 21:30:43 字数 1659 浏览 7 评论 0原文

在描述问题之前,让我首先指出,这是一个与 这个问题

问题

此屏幕截图是在 tableView:didSelectRowAtIndexPath 设置了中断的情况下拍摄的: ,正如您在模拟器中看到的那样(图像最右侧),所选单元格的底部有一条单像素蓝线。这不是客户要求的设计,也不是这个应用程序过去的行为方式:即使在选择时也不应该有分隔符。

我是如何来到这里的 我最初使用自定义 UITableViewCell 类和相应的 nib (.xib) 文件设计了这个表格视图,并且选择时没有遇到任何问题:分隔符根据需要隐藏。可以预见的是,由于视图层次结构的所有开销,滚动速度很慢,因此我重新设计了自定义单元格以使用 Loren Brichter 的 快速滚动解决方案。现在滚动速度更快了,但我一生都无法摆脱分隔符。

我尝试过的

在上面的屏幕截图时...

  • 表视图在 IB 中具有“分隔符[无]”。
  • 包含表视图的 UIViewController 在 vi​​ewDid Load 中有这一行: self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

正如您在屏幕截图中看到的,我插入了一些未使用的行来证明分隔符样式已根据需要设置。其他测试确认 tableView 和 self.tableView 在同一断点处是等效的指针。

我还尝试将 tableView.separatorColor 设置为黑色并清除,所有结果都相同:在做出选择之前,单元格看起来都是正确的。


Manjunath:这是我用来根据单元格是否被触摸来绘制替代背景的代码。您可以在屏幕截图中看到差异(动画时不那么微妙)。

if(self.highlighted) {
    textColor = [UIColor blackColor];
    UIImage *bg = [UIImage imageNamed:@"image-cell-background_highlighted.png"];
    [bg drawAtPoint:CGPointMake(0.0, 1.0)];
}
else {
    UIImage *bg = [UIImage imageNamed:@"image-cell-background.png"];
    [bg drawAtPoint:CGPointMake(0.0, 0.0)];
}

这在 UIImageCell.m 中的 drawContentView: 中被调用,该方法继承自 Brichter 先生的 ABTableViewCell 超类。

Before describing the problem, let me first point out that this is a distinct issue from this question.

The Problem

This screenshot was taken with a break set at tableView:didSelectRowAtIndexPath:, and as you can see in the simulator (far right of the image), there's a single-pixel blue line at the bottom of the selected cell. This is not the design asked for by the client, nor is it how this app used to behave: there should be no separator, even on selection.

How I Got Here
I'd initially designed this table view using custom UITableViewCell classes with corresponding nib (.xib) files and had no trouble with selections: the separator was hidden as desired. Predictably, scrolling was sluggish due to all the overhead from the view hierarchy, so I reworked the custom cells to use Loren Brichter's fast scrolling solution. Now scrolling is much faster, but I can't get rid of the separator for the life of me.

What I've tried

At the time of the screenshot above...

  • the table view has "Separator [None]" in IB.
  • the UIViewController that contains the table view has this line in viewDid Load: self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

As you can see in the screenshot, I inserted some unused lines to prove that separatorStyle is set as desired. Other testing confirms that tableView and self.tableView are equivalent pointers at that same breakpoint.

I've also tried setting tableView.separatorColor to black and to clear, all with the same result: the cells look right until a selection is made.


Manjunath: Here's the code I'm using to draw alternate backgrounds depending on whether the cell's been touched or not. You can see the difference—which is less subtle when animated—in the screenshot.

if(self.highlighted) {
    textColor = [UIColor blackColor];
    UIImage *bg = [UIImage imageNamed:@"image-cell-background_highlighted.png"];
    [bg drawAtPoint:CGPointMake(0.0, 1.0)];
}
else {
    UIImage *bg = [UIImage imageNamed:@"image-cell-background.png"];
    [bg drawAtPoint:CGPointMake(0.0, 0.0)];
}

This gets called in UIImageCell.m in drawContentView:, a method inherited from Mr. Brichter's ABTableViewCell super class.

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

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

发布评论

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

评论(2

故事未完 2024-09-07 21:30:43

Chris,

深入研究 ABTableViewCell,我发现:

- (void)setFrame:(CGRect)f
{
 [super setFrame:f];
 CGRect b = [self bounds];
 b.size.height -= 1; // leave room for the seperator line
 [contentView setFrame:b];
}

由于单元格的高度比实际单元格短一像素,因此当选择单元格时,该一像素线将以选择颜色的颜色渗透。它可能看起来像是分隔符,但实际上是选择颜色。

要进行测试,请尝试将上面的线更改为两个像素或更短,看看会发生什么。

更新:

通过对 FastScrollingExample 项目的 -rootViewController: 进行此更改,

- (void)viewDidLoad
{
 self.title = @"Fast Scrolling Example";
 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [super viewDidLoad];
}

并在 -drawContentView 中注释掉:

// if(self.selected)
// {
//  backgroundColor = [UIColor clearColor];
//  textColor = [UIColor whiteColor];
// }
// 

以模拟没有显示选择颜色时会发生的情况,然后我得到这样的屏幕截图:

alt text http://files.me.com/mahboud/ 7k656q

看起来很眼熟吗?

你会如何解决这个问题?如果不需要选择单元格,请禁用单元格选择。否则,如果您要选择单元格,则应该使矩形变大,以便当您在 -drawConentRect 中使用自己的选择颜色进行绘制时,默认选择颜色不会显示出来。

Chris,

Delving into ABTableViewCell, I see:

- (void)setFrame:(CGRect)f
{
 [super setFrame:f];
 CGRect b = [self bounds];
 b.size.height -= 1; // leave room for the seperator line
 [contentView setFrame:b];
}

Since the height of the cell is one pixel shorter than the actual cell, when the cell gets selected, that one-pixel line will bleed through in the color of the selection color. It may look like it's the separator, but it is actually the selection color.

To test, try to change that line above to be two pixels or more shorter to see what happens.

Update:

By making this change to the FastScrollingExample project's -rootViewController:

- (void)viewDidLoad
{
 self.title = @"Fast Scrolling Example";
 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [super viewDidLoad];
}

and commenting out:

// if(self.selected)
// {
//  backgroundColor = [UIColor clearColor];
//  textColor = [UIColor whiteColor];
// }
// 

in -drawContentView to mimic what would happen if you didn't have the selection color showing through, then I get a screen shot like this:

alt text http://files.me.com/mahboud/7k656q

Look familiar?

How would you get around this? If you don't need to select cells, then disable cell selection. Otherwise, if you are selecting cells, then you should make the rect larger so the default selection color doesn't show through when you paint with your own selection color in -drawConentRect.

寂寞陪衬 2024-09-07 21:30:43

试试这个:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

 // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {

        static NSString *CellIdentifier = NSLocalizedString(@"Cell",@"");
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (nil == cell)
        {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }   
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }

Try this:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

 // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {

        static NSString *CellIdentifier = NSLocalizedString(@"Cell",@"");
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (nil == cell)
        {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }   
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文