from.. 2009-04-07 09:54 采纳率: 100%
浏览 314
已采纳

Uitableview-scroll to the top

In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section number 5.

So I get an exception, when I call:

[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

Is there another way to scroll to the top of table view?

转载于:https://stackoverflow.com/questions/724892/uitableview-scroll-to-the-top

  • 写回答

30条回答 默认 最新

  • 喵-见缝插针 2009-04-07 10:23
    关注

    UITableView is a subclass of UIScrollView, so you can also use:

    [mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
    

    Or

    [mainTableView setContentOffset:CGPointZero animated:YES];
    

    And in Swift:

    mainTableView.setContentOffset(CGPointZero, animated:true)
    

    And in Swift 3 & above:

    mainTableView.setContentOffset(.zero, animated: true)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试