【发布时间】:2015-04-28 15:01:47
【问题描述】:
您可以在下面的 gif 中看到 UITableView 单元格内容的第一次滚动时移动了一点点。你几乎看不到它,边距变宽了 1 个像素。我以前从未遇到过这种情况。似乎在第一次滚动之前存在一些布局问题,并且在事后自行解决。 XCode 中没有警告,这些自定义单元格非常简单,没有布局代码覆盖。
我不知道从哪里开始,我该如何发现这个故障?
更新。我现在已经实施了一个明显的解决方法:
- (void)scrollTableToFixGlitch {
[self.tableView setContentOffset:CGPointMake(0, 1)];
[self.tableView setContentOffset:CGPointMake(0, -1)];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self scrollTableToFixGlitch];
}
仍在调查问题。我试过通用的 UITableViewCells,没有任何改变。似乎是 View Controller 的根视图或 tableview 布局的问题,而不是表格单元格的问题。
更新 2。
我排除了所有动画问题,问题出在不同区域的某个地方。在一个非常简化的项目上很容易重新创建故障。我的标签栏控制器基于MHCustomTabBarController,带有自定义segues 和其他一些附加功能。以下是重现故障的方法。设置一个项目,您的初始 VC 嵌入到导航控制器中。下一个控制器 MHCustomTabBarController 或子类被推送到导航堆栈。标签栏中第一个可见的 VC 是通用表视图控制器。就是这样。仅当标签栏控制器被推入导航堆栈时才会出现故障。
这是我认为标签栏控制器的一些代码:
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.childViewControllers.count < 1) {
[self performSegueWithIdentifier:@"viewController1" sender:[self.buttons objectAtIndex:0]];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if (![segue isKindOfClass:[MHTabBarSegue class]]) {
[super prepareForSegue:segue sender:sender];
return;
}
self.oldViewController = self.destinationViewController;
//if view controller isn't already contained in the viewControllers-Dictionary
if (![self.viewControllersByIdentifier objectForKey:segue.identifier]) {
[self.viewControllersByIdentifier setObject:segue.destinationViewController forKey:segue.identifier];
}
[self.buttons setValue:@NO forKeyPath:@"selected"];
[sender setSelected:YES];
self.selectedIndex = [self.buttons indexOfObject:sender];
self.destinationIdentifier = segue.identifier;
self.destinationViewController = [self.viewControllersByIdentifier objectForKey:self.destinationIdentifier];
[[NSNotificationCenter defaultCenter] postNotificationName:MHCustomTabBarControllerViewControllerChangedNotification object:nil];
}
还有一个自定义的 segue 代码:
@implementation MHTabBarSegue
- (void) perform {
MHCustomTabBarController *tabBarViewController = (MHCustomTabBarController *)self.sourceViewController;
UIViewController *destinationViewController = (UIViewController *) tabBarViewController.destinationViewController;
//remove old viewController
if (tabBarViewController.oldViewController) {
[tabBarViewController.oldViewController willMoveToParentViewController:nil];
[tabBarViewController.oldViewController.view removeFromSuperview];
[tabBarViewController.oldViewController removeFromParentViewController];
}
destinationViewController.view.frame = tabBarViewController.container.bounds;
[tabBarViewController addChildViewController:destinationViewController];
[tabBarViewController.container addSubview:destinationViewController.view];
[destinationViewController didMoveToParentViewController:tabBarViewController];
}
@end
更新 3
在我的研究中,我发现 - viewWillAppear 在子控制器第一次出现时不会被调用。但在所有后续时间中都会调用它。
【问题讨论】:
标签: ios uitableview autolayout