3

テーブルビューに含まれるセルが10個程度未満の場合、UITableViewCellを強制的に一番上にスクロールするにはどうすればよいですか?tableviewが編集モードのときに、tableViewセル内の管理対象オブジェクトコンテキストの編集をサポートします。言うまでもなく、セルがテーブルビューの下部にある場合、ユーザーがイベントのタイトルまたは場所を編集しようとすると、そのセルはキーボードによってブロックされます。私は次のような解決策を試しました:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if(_selectedIndex == indexPath.row){
        _selectedIndex = -1;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        return;
    }

    if(_selectedIndex >= 0){
        NSIndexPath *previous = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
        _selectedIndex = indexPath.row;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previous]
                         withRowAnimation:UITableViewRowAnimationFade];
    }

    _selectedIndex = indexPath.row;
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];

    [tableView setContentOffset:CGPointMake(0, [tableView rowHeight]*indexPath.row) animated:YES];
}

ただし、これはテーブルビューをcontentOffsetに保持しません。上部にスナップしてから元に戻ります

4

8 に答える 8

12

次のソリューションが気に入っています。ヘッダービューがある場合は、ヘッダービューの一番上までスクロールするからです。

[self.tableView setContentOffset:CGPointZero animated:YES];
于 2013-10-17T07:19:36.867 に答える
4

UITableViewでこのメソッドを使用する必要があります。

-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

基本的には、UITableViewDelegateの[tableView:didSelectRowAtIndexPath:]メソッドが呼び出されたときに、その選択のインデックスを上記のメソッドに渡し、scrollposition=UITableViewScrollPositionTopを設定します。

これにより、選択したセルが画面の上部にスクロールされ、キーボードの邪魔になりません。

于 2012-06-29T22:04:50.093 に答える
2

テーブルビュー全体を上にスライドすることを検討しましたか?

編集:私はあなたが探しているものを実行するように見える何かを見つけました: ここにコードを書いてください。

このコードは、テーブルビューの挿入図を下から縮小します。これにより、下にスナップすることなく、下のセルの1つを表示できます。

于 2012-06-29T21:28:59.310 に答える
2

UITableViewはUIScrollViewのサブクラスであるため、次のものも使用できます。

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
于 2013-12-24T10:48:20.037 に答える
0

これを試しましたか?:

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

サンプルは次のとおりです。

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:0 animated:YES];
于 2014-04-16T18:34:35.350 に答える
0

これは私の場合は機能します。また、次の場合は、headearの一番上までスクロールします。

  **myTablView.scrollRectToVisible((myTablView.rectForHeader(inSection: 0), to: myTablView.superview).0, animated: true)**

https://developer.apple.com/documentation/uikit/uitableview/1614872-rectforheader

于 2017-09-07T16:03:27.780 に答える
0

一番上までスクロールすると便利ですtableView

tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true).
于 2019-05-27T10:19:28.273 に答える
0

あなたの質問に対するより具体的な答え:

extension UITableView {

    func scrollToBottom(){

        DispatchQueue.main.async {
            let indexPath = IndexPath(
                row: self.numberOfRows(inSection:  self.numberOfSections -1) - 1, 
                section: self.numberOfSections - 1)
            self.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }

    func scrollToTop() {

        DispatchQueue.main.async {
            let indexPath = IndexPath(row: 0, section: 0)
            self.scrollToRow(at: indexPath, at: .top, animated: false)
        }
    }
}
于 2019-05-27T10:49:54.007 に答える