我有一个类似于iPhone的Contact应用程序工作方式的应用程序。当我们添加新的联系人时,用户将被定向到包含联系信息的仅查看屏幕。如果我们选择“所有联系人”,在导航栏中,用户将导航到最近添加的联系人所在的所有联系人列表。

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

    [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, e.g. 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];
    }
}

我希望这会有所帮助。如果您在列表中间添加一些内容,则可以轻松滚动到该位置。

其他提示

你可以尝试这个,当我点击uitableview的按钮滚动时,我的应用程序与你类似。

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

10表示要向上滚动的单元格数

希望这会对你有所帮助: - )

当从 viewDidAppear(_:)设置偏移时,滚动动画是不可避免的。设置初始滚动偏移的更好位置是 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)
    }
}

您是否添加了足够的虚拟联系人来测试滚动?似乎只有当你的tableView具有一定的大小时,iPhone才会找到要滚动的输入法。

这是在我的项目中适合我的代码。我使用前一个按钮,因此我将桌面视图进一步向下滚动,通常会使用UITABLEVIEWSCROLLPOSITION。 (如果我的上一个按钮不能“看到”前一个单元格,那么我的上一个按钮将不起作用。

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

- (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];

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top