在UITableView中设置滚动位置

我有一个应用程序,它的作用与iPhone的联系人应用程序的工作方式类似。 当我们添加一个新的联系人用户被引导到一个仅查看联系人信息屏幕。 如果我们从导航栏中select“所有联系人”,用户将被导航到最近添加的联系人所在的所有联系人列表。

我们可以使用以下方法将视图移动到特定的行:

[itemsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom]; 

…但它不工作。 我在打电话后立即打电话给他:

  [tableView reloadData]; 

我想我不想在这里调用selectRowAtIndexPath:animated:scrollPosition方法。 但如果不在这里,那么在哪里?

是否有任何委托方法被调用以下方法后?

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

我想我有一个类似的应用程序:项目列表,点击'+' – >新的屏幕,回来看看更新列表,滚动显示在底部添加的项目。

总之,我把reloadData放在viewWillAppear:animated:scrollToRowAtIndexPath:...viewDidAppear:animated:

 // Note: Member variables dataHasChanged and scrollToLast have been // set to YES somewhere else, eg when tapping 'Save' in the new-item view. - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (dataHasChanged) { self.dataHasChanged = NO; [[self tableView] reloadData]; } else { self.scrollToLast = NO; // No reload -> no need to scroll! } } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (scrollToLast) { NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0]; [[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } } 

我希望这有帮助。 如果你在列表中间添加一些东西,你可以很容易地滚动到那个位置。

你可以尝试这个,我的应用程序在某种类似于你,当我点击一个button滚动的可视化是起来的。

 [tableviewname setContentOffset:CGPointMake(0, ([arrayofcontact count]-10)*cellheight) animated:YES]; 

10是你想要向上滚动多less个单元格

希望对你有帮助:-)

你有足够的虚拟联系人添加testing滚动? 看来只有当你的tableView是一定大小的iPhonefindinputus滚动。

这是在我的项目中为我工作的代码。 我使用前一个button,因此我将tableview向下滚动一点,通常会使用UITABLEVIEWSCROLLPOSITION。 (我以前的button将无法正常工作,如果它不能“看到”前一个单元格。

忽略一些自定义的方法调用。

 - (void)textFieldDidBeginEditing:(UITextField *)textField { //Show the navButtons [self navButtonAnimation]; DetailsStandardCell *cell = (DetailsStandardCell *)textField.superview.superview.superview; self.parentController.lastCell = cell; //Code to scroll the tableview to the previous indexpath so the previous button will work. NOTE previous button only works if its target table cell is visible. NSUInteger row = cell.cellPath.row; NSUInteger section = cell.cellPath.section; NSIndexPath *previousIndexPath = nil; if (cell.cellPath.row == 0 && cell.cellPath.section != 0) //take selection back to last row of previous section { NSUInteger previousIndex[] = {section -1, ([[self.sections objectForKey:[NSNumber numberWithInt:section - 1]]count] -1)}; previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2]; } else if (cell.cellPath.row != 0 && cell.cellPath.section != 0) { NSUInteger previousIndex[] = {section, row - 1}; previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2]; } [self.theTableView scrollToRowAtIndexPath: cell.cellPath.section == 0 ? cell.cellPath : previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; } 

当从viewDidAppear(_:)设置偏移时,滚动animation是不可避免的。 设置初始滚动偏移的更好的地方是viewWillAppear(_:) 。 你需要强制一个表视图的布局,因为它的内容大小没有定义在这一点上。

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) tableView.setNeedsLayout() tableView.layoutIfNeeded() if let selectedIndexPath = selectedIndexPath, tableView.numberOfRows(inSection: selectedIndexPath.section) > 0 { tableView.scrollToRow(at: selectedIndexPath, at: .top, animated: false) } }