setContentOffset仅在动画设置为YES时有效

She*_*lam 5 iphone cocoa-touch objective-c uitableview

我有一个UITableView,我想向下显示100px。由于某些原因,这仅在将animated设置为YES时有效。为什么是这样?

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    /*[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] 
                          atScrollPosition:UITableViewScrollPositionNone 
                                  animated:NO];*/

    /*[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1] 
                                        atScrollPosition:UITableViewScrollPositionNone 
                                        animated:NO];*/

    [self.tableView setContentOffset:CGPointMake(0,100) animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

And*_*rei 0

如果您尝试使用 setFrame 而不是 setContentOffset 会怎样?

您应该在 viewDidAppear 方法中添加以下代码:


[self.tableView setFrame:CGRectMake(0, 100, 320, 380)];
Run Code Online (Sandbox Code Playgroud)

根据视图中的其他元素(例如导航控制器、工具栏等),您必须将 380 调整为其他内容。