iOS7中所选UITableViewCell的奇怪行为

时间:2013-10-13 08:52:04

标签: ios uitableview ios7

我正在将我的应用程序移植到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;

现在,我知道其他人已经问过这个问题(或者至少是其中的一部分),但没有一个答案对我有用。为了进一步参考,我尝试了这些“解决方案”,他们做了 NOT 工作:

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;

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,为我修复它的方法是覆盖UITableViewCell子类中的layoutSubviews,如下所示:

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

重要的是首先进行[super layoutSubviews]来电。

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