iOS7中UITableViewCell的选择行为异常

4

我正在将我的应用程序移植到 iOS 7 平台,并且在我的 tableView 中遇到了一些奇怪的行为。当我 选择一个单元格时,分隔符会出现无法解释的消失。不仅如此,选中状态的背景图片也会向上移动约 1 像素,尽管其框架并未指示此问题。在 iOS 6 或更早的操作系统中我没有遇到这些问题。

我按照以下方式定义了我的表视图:

    statisticsTableView = [[UITableView alloc] initWithFrame:CGRectMake(170.0f, 0.0f, 150.0f, window.size.height) style:UITableViewStylePlain];
    statisticsTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    statisticsTableView.separatorColor = [UIColor darkGrayColor];
    //statisticsTableView.separatorInset = UIEdgeInsetsZero; // <- Makes app crash in iOS6 or older
    statisticsTableView.showsHorizontalScrollIndicator = NO;
    statisticsTableView.delegate = self;
    statisticsTableView.dataSource = self;

这个单元格被子类化,相关的部分如下(正常和选中的背景图像大小完全相同,在iOS6或更早版本中显示正确,只有在iOS 7中选定的背景图像稍微高一些):

    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
    backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_background_corp.png"]];
    self.backgroundView = backgroundView;

    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
    backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_background_corp_selected.png"]];
    self.selectedBackgroundView = backgroundView;

现在,我知道其他人已经提出了这个问题(或者至少其中的一部分),但是那里的答案都没有对我起作用。供参考,我尝试过这些“解决方案”,它们都没有起作用:

Putting this into my didSelectrowAtIndexPath made my cells deselect. 
But, I need my selected cell to stay selected, and besides 
it looked really awkward with the blinking as swithces between states:

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

另一个:
Putting this into the heightForRowAtIndexPath delegate method 
as a return value did absolutely nothing:

    ceilf(rowHeight);

最后:
This was my attempt to fix the background image position bug. I modified
the way I added the selected background view in the subclassed tableView cell,
but it did not do squat:

    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
    if (iOSVersion >= 7.0f)
    {
        backgroundView.frame = CGRectMake(0.0f, 1.0f, 150.0f, 70.0f);
    }
    backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_background_corp_selected.png"]];
    self.selectedBackgroundView = backgroundView;

4
我也见过内置的苹果应用程序出现这种情况。虽然不是每次都发生,但足够频繁以至于引起注意。 - Jason C. Howlin
1
也许这是一个将在未来更新中解决的错误。 - user1799795
你是使用默认的TableView单元格还是自定义的呢? - Satyam
这个问题是在iOS 7上吗?如果是的话,请尝试将表格视图类型设置为普通表格。这可能会解决问题。 - user821127
1个回答

1

我曾经遇到过类似的问题,解决方法是在UITableViewCell子类中重写layoutSubviews方法,如下所示:

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.selectedBackgroundView.frame = self.backgroundView.frame;
}

重要的是首先调用[super layoutSubviews]
顺便说一下,我认为iOS7中的默认行为是有意的。如果您查看苹果邮件应用程序,会发现在选择单元格时分隔线也会消失,并且活动背景会绘制在分隔线原来的位置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接