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