【问题标题】:setContentOffset not scrolling my UITableViewsetContentOffset 不滚动我的 UITableView
【发布时间】:2014-02-01 08:17:51
【问题描述】:

当所选单元格到达 UITableView 中的最后一个位置时,我试图将 UITableViewCell 向上滚动一个 UITableViewCell 位置

我有正确的逻辑来识别 UITableView 的最后一个可见 UItableViewCell。但是,使 UITableView 向上滚动一个位置的代码不起作用。这是我写的代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cellSelectedBool = YES;

    cell = (CustomFinishingCell *)[tableView cellForRowAtIndexPath:indexPath]; // error
    [cell.widthTexField becomeFirstResponder];


    // Gets an array of current visible cells in UITableView
    NSArray *visibleCells = [tableView visibleCells];
    NSLog(@"%@", visibleCells);
    // get indexpath of last cell
    UITableViewCell *lastCell = [visibleCells lastObject];
    NSIndexPath *lastCellIndex = [finishingTableView indexPathForCell:lastCell];
    // perform scroll when last visible cell is selected
    if (indexPath.row == lastCellIndex.row) {
        NSLog(@"BIGGER");
        int cellHeight = 44;
        [UIView animateWithDuration:.25 animations:^{
            [finishingTableView setContentOffset:CGPointMake(finishingTableView.contentOffset.x,finishingTableView.contentOffset.y + cellHeight) animated:YES];
        }];
    }

【问题讨论】:

  • 我认为您正在尝试向下滚动它(它不能再进一步了......)。试试finishingTableView.contentOffset.y - cellHeight

标签: ios objective-c uitableview


【解决方案1】:

你应该使用:

NSIndexPath *rowToSelect;  // assume this exists and is set properly
UITableView *myTableView;  // assume this exists

[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNone animated:YES];

【讨论】:

  • 好吧,这行得通....但是,如果用户在 UIKeyBoard 上按回车键并且当前位于最后一行,则不会调用 didSelectRowAtIndexPath .. 但是,如果我用手指选择最后一行,它将调用 didSelectRowAtIndexPath 然后工作.....
【解决方案2】:
if (indexPath.row == lastCellIndex.row) {
    [finishingTableView scrollToRowAtIndexPath:indexPathatScrollPosition:
        UITableViewScrollPositionNone animated:YES];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多