4

我正在将我的应用程序移植到 iOS 7 并且我的 tableViews 有一些奇怪的行为。当我选择一个单元格时,分隔符会无缘无故消失。不仅如此,选定状态的背景图像也会向上移动大约 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 选定的 BG 图像显示稍高):

    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

1 回答 1

1

我遇到了类似的问题,为我解决的问题是在 UITableViewCell 子类中覆盖 layoutSubviews,如下所示:

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

重要的是先[super layoutSubviews]打电话。

顺便说一句,我认为默认行为是针对 iOS7 的。如果您查看 Apple Mail 应用程序,您会看到分隔线在选择单元格时也消失了,并且活动背景绘制在分隔线所在的位置。

于 2014-05-17T09:12:44.963 回答