【问题标题】:Get a UITableView to scroll up from the bottom / snap to the bottom, not the top获取一个 UITableView 从底部向上滚动/捕捉到底部,而不是顶部
【发布时间】:2010-01-11 05:07:10
【问题描述】:

我有一个表格视图,很多情况下只有一两个单元格不会填满屏幕。在这种情况下,我希望细胞位于底部,而不是顶部。换句话说,它们应该“捕捉”到 tableview 的底部。

我可以像这样强制表格视图将它们滚动到底部:

  CGPoint bottomOffset = CGPointMake(0, [self.tableView contentSize].height - self.tableView.frame.size.height);
  [self.tableView setContentOffset:bottomOffset animated:NO];

但是,这只是部分成功。首先,如果我把它放在 viewDidLoad 或 viewWillAppear 中不起作用,只放在 viewDidAppear 中,这意味着用户首先看到的 tableview 的单元格位于顶部,然后它们移动到底部。其次,如果他们滚动表格视图,当他们松开时,它会自动“捕捉”回顶部。

有谁知道如何改变这种行为?

【问题讨论】:

    标签: iphone uitableview scroll


    【解决方案1】:

    要考虑的一个选项是根据要显示的行数调整 UITableView 本身的大小。假设您的 UITableViewDelegate 实现了 heightForRowAtIndexPath ,则可以通过将行数乘以每行的高度来在 viewWillAppear 方法中设置 UITableView 的高度。

    类似这样的:

    CGRect frame = [myTableView frame];
    
    frame.size.height = [[myTableView dataSource] tableView: myTableView numberOfRowsInSection: 0] *
                        [[myTableView delegate] tableView: myTableView heightForRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0]];
    
    [myTableView setFrame: frame];
    

    此示例假设您的表格有一个部分,并且每一行的高度相同。如果涉及多个部分或不同的行可能具有不同的高度,则计算大小将变得更加复杂。

    不管高度是如何计算的,这个想法的本质是让表格本身更短,不比它所显示的一两行高,而不是试图强迫它表现得不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-13
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多