如何解决为“分组”的UITableView附加的保证金调整代码?

试图实现分组的TableView的外观和感觉,每个部分只有一个项目,但几乎没有任何边距(使用户能够select他们想要的颜色的能力)。

我有它的工作,但是当用户改变方向时,我不得不使用didRotateFromInterfaceOrientation方法(因为willRotateToInterfaceOrientation不起作用),但效果是,你看到在tableView显示后,在这一小部分秒内边缘变化很快。

问题 – 任何解决问题的方法都不会看到这种转变?

- (void) removeMargins { CGFloat marginAdjustment = 7.0; CGRect f = CGRectMake(-marginAdjustment, 0, self.tableView.frame.size.width + (2 * marginAdjustment), self.tableView.frame.size.height); self.tableView.frame = f; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self removeMargins]; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; [self removeMargins]; } 

我认为这个问题是,在willRotateToInterfaceOrientation表框的框架还没有调整,所以你的框架计算是不正确的。 在didRotateFromInterfaceOrientation框架已经改变。

我认为解决这个问题最简单的方法是inheritanceUITableView并重写layoutSubviews。 每当视图的框架以可能要求其子视图改变的方式改变时,该方法被调用。

下面的代码为我工作没有animation故障:

 @interface MyTableView : UITableView { } @end @implementation MyTableView -(void) layoutSubviews { [super layoutSubviews]; CGFloat marginAdjustment = 7.0; if (self.frame.origin.x != -marginAdjustment) // Setting the frame property without this check will call layoutSubviews again and cause a loop { CGRect f = CGRectMake(-marginAdjustment, 0, self.frame.size.width + (2 * marginAdjustment), self.frame.size.height); self.frame = f; } } @end 

你说willRotateToInterfaceOrientation不起作用,但你没有说明为什么。

如果原因是willRotateToInterfaceOrientation没有被调用,那么请看看这个问题 。

如果你得到这个工作,那么我相信你的其他问题将解决自己。