如何修复为“Grouped”UITableView附加的边距调整代码?

时间:2011-10-05 12:07:15

标签: iphone ios uitableview

努力实现外观和效果Grouped TableView的感觉,每个部分只有一个项目,但几乎没有任何边距(给我圆角的视图,用户能够选择他们想要的颜色)。

我有它工作,但是当用户改变方向时我必须使用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];
}

2 个答案:

答案 0 :(得分:1)

我认为问题是在willRotateToInterfaceOrientation中,tableView的框架尚未调整大小,因此您的框架计算不正确。在didRotateFromInterfaceOrientation上,框架已经改变。

我认为解决这个问题的最简单方法是继承UITableView并覆盖layoutSubviews。每当视图的框架以可能需要更改子视图的方式更改时,都会调用此方法。

以下代码适用于我而没有动画故障:

@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

答案 1 :(得分:0)

你说willRotateToInterfaceOrientation没有用,但你没有说明原因。

如果原因是没有调用willRotateToInterfaceOrientation,那么请查看this question

如果你的工作正常,那么我相信你的other question会自行解决。