27

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

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

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

...但它不起作用。我打电话后马上打电话:

    [tableView reloadData];

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

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

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

4 回答 4

61

我想我有一个类似的应用程序:项目列表,点击“+”-> 新屏幕,返回并查看更新的列表,滚动以在底部显示添加的项目。

总而言之,我输入reloadDataviewWillAppear: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];
    }
}

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

于 2009-01-20T04:07:59.790 回答
6

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

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

10 表示您要向上滚动多少个单元格

希望对你有帮助:-)

于 2012-04-04T07:56:39.523 回答
2

当从 设置偏移量时,滚动动画是不可避免的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)
    }
}
于 2017-01-19T09:42:38.823 回答
0

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

这是在我的项目中对我有用的代码。我使用上一个按钮,因此我将 tableview 向下滚动一点,它通常会与 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];

}
于 2009-01-05T15:22:16.367 回答