web-dev-qa-db-ja.com

UITableViewを特定の位置にスクロールする方法

テーブルのセルを特定の位置にスクロールするにはどうすればよいですか? 3行(高さによる)を示すテーブルがあります。私が望むのは、テーブルの高さに応じて1行目をクリックすると、1行目がスクロールして新しい位置(中央)になり、他の行でも同じになることです。 contenOffsetを試しましたが、うまくいきませんでした。

編集済み:

要するに、ピッカーで行を選択すると、データピッカーのように、行が中央にスクロールします。

ありがとう。

58
Maulik

最終的に私は見つけました...テーブルが3行しか表示されない場合はうまくいきます...行がさらに変更される場合はそれに応じて...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{    
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{  
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {         
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    cell.textLabel.text =[NSString stringWithFormat:@"Hello roe no. %d",[indexPath row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * theCell = (UITableViewCell *)[tableView     
                                              cellForRowAtIndexPath:indexPath];

    CGPoint tableViewCenter = [tableView contentOffset];
    tableViewCenter.y += myTable.frame.size.height/2;

    [tableView setContentOffset:CGPointMake(0,theCell.center.y-65) animated:YES];
    [tableView reloadData]; 
 }
15
Maulik

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animatedを使用して動作するはずです。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[yourTableView scrollToRowAtIndexPath:indexPath 
                     atScrollPosition:UITableViewScrollPositionTop 
                             animated:YES];

atScrollPositionは次の値のいずれかを取ることができます。

typedef enum {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
} UITableViewScrollPosition;

これがあなたのお役に立てば幸いです

乾杯

134
Fran Sevillano
[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

これにより、テーブルビューが最初の行に移動します。

17
Praveen S

つかいます [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:YES];インデックスパスで識別される行が画面上の特定の場所に来るまで、レシーバーをスクロールします。

そして

scrollToNearestSelectedRowAtScrollPosition:animated:

テーブルビュー内の指定された位置に最も近い選択された行がその位置になるように、テーブルビューをスクロールします。

14
visakh7

Swift 4.2バージョン:

let indexPath:IndexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .none, animated: true)

列挙:これらは、利用可能なtableViewスクロール位置です-ここでは参照用です。このセクションをコードに含める必要はありません。

public enum UITableViewScrollPosition : Int {

case None
case Top
case Middle
case Bottom
}

DidSelectRow:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let theCell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)

    if let theCell = theCell {
        var tableViewCenter:CGPoint = tableView.contentOffset
        tableViewCenter.y += tableView.frame.size.height/2

        tableView.contentOffset = CGPointMake(0, theCell.center.y-65)
        tableView.reloadData()
    }

}
9
odemolliens

setContentOffsetアプローチを使用すると、テーブルビュー/コレクションビューが少しジャンプする可能性があることに注意してください。正直に言って、この別の方法を試してみます。推奨事項は、無料で提供されるスクロールビューデリゲートメソッドを使用することです。

1
Stephen Paul

単にコードの1行:

self.tblViewMessages.scrollToRow(at: IndexPath.init(row: arrayChat.count-1, section: 0), at: .bottom, animated: isAnimeted)
0