繁体   English   中英

如何将 UITableView 滚动到特定位置

[英]How to scroll UITableView to specific position

如何将表格的单元格滚动到特定位置? 我有一个显示 3 行(根据高度)的表格。 我想要的是,如果我点击第一行而不是根据表格的高度,第一行应该滚动并获得新的位置(中心),其他行也是如此。 我试过 contenOffset 但没有用..

编辑:

简而言之,当我们在选择器中选择任何行时,就像数据选择器一样,该行会滚动到中心。

谢谢..

它应该使用- (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;

我希望这可以帮助你

干杯

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

这会将您的表格视图带到第一行。

最后我发现......当表格只显示 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]; 
 }

使用[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:YES]; 滚动接收器,直到由索引路径标识的行位于屏幕上的特定位置。

scrollToNearestSelectedRowAtScrollPosition:animated:

滚动表格视图,以便在表格视图中最接近指定 position 的选定行位于该 position 处。

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
}

选择行:

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()
    }

}

值得注意的是,如果你使用setContentOffset方式,可能会导致你的表格视图/集合视图有点跳跃。 老实说,我会以另一种方式尝试 go。 建议使用免费提供的滚动视图委托方法。

只需一行代码:

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

如果您的表视图只有一行和一个部分,您也可以使用以下方法

 challengeInfoTableView.scrollRectToVisible(challengeInfoCell.challengeDescriptionTextView!.frame, animated: false)

在这种情况下,您可以直接滚动到正确的文本字段、文本视图、标签等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM