【问题标题】:UITableView does not PROPERLY scroll to bottom programmaticallyUITableView 不能以编程方式正确滚动到底部
【发布时间】:2015-04-02 00:27:29
【问题描述】:

我有一个 100 行的表格,想以编程方式将其滚动到任何元素并选择该元素。在第一个控制器中,我指定行和部分,并将带有 UITableView 的控制器推送到导航堆栈上。在“tableView-controller”的viewWillAppear 中,我使用代码:

[tableView_ selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];

奇怪的是,它完美地滚动到 99 个第一行中的任何一个,第 100 行被选中,但仍然“低于”可见区域。我必须手动滚动表格才能显示它。

如果我将该代码放在viewDidAppear 中,表格可以很好地滚动,但会出现明显的延迟。

我还尝试了什么:

  1. 降低tableView的高度。我得到了 tableView,它部分覆盖了屏幕,最后一行确实被遮挡了。
  2. 使用 contentSize - 将其高度设置为小而大的值 - 不走运。
  3. 设置 contentInset.bottom - 有效,但我在底部有一个无用的空间。
  4. viewDidAppear中,照常处理99行并为第100行设置自定义contentOffset

    offset.y += tableView_.contentSize.height - tableView_.bounds.size.height + 44;
    

    也有效,但它似乎是一种丑陋的方式。

那么,当“tableView-controller”出现时,我应该怎么做才能让我的 tableView 预滚动到第 100 个元素?

更新。 我不使用任何 xib 或故事板,只是在 viewDidLoad 中定义视图:

- (void)viewDidLoad {
  [super viewDidLoad];

  tableView_ = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
  tableView_.dataSource = self;
  tableView_.delegate = self;
  tableView_.sectionFooterHeight = 0.0f;

  self.view = tableView_;

  numbers = [NSMutableDictionary dictionary];
  numberSectionTitles = [NSMutableArray array];

  //... some model initialization

  headerView = [[UIView alloc] initWithFrame:(CGRect){0, 0, 320, 60}];
  headerView.backgroundColor = [UIColor greenColor];

  UILabel *l = [[UILabel alloc] initWithFrame:(CGRect){20, 20, 280, 20}];
  l.textColor =[UIColor whiteColor];
  l.tag = 121;

  [headerView addSubview:l];
}

并在 viewDidAppear 中调整选择(肮脏的解决方法)

-(void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
    int row = _selectedIndex  % 10;
    int section = (_selectedIndex - row) / 10;

  if(row == 9 && section == 9){
    CGSize contentSize = tableView_.contentSize;
    CGPoint offset = tableView_.contentOffset;

    offset.y += tableView_.contentSize.height - tableView_.bounds.size.height + 44;
    tableView_.contentOffset = offset;
  }
  [tableView_ selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];

}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    可能是您的视图高度与 tableview 不同。您的超级视图(您要在其上添加表格视图)应该是相同的。您的最后一行被隐藏了,因为您的视图与您的 tableview 高度不同,这可能会解决您的问题。

    【讨论】:

    • 谢谢,有用的提示,但我的 tableview 是控制器的视图:tableView_ = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];self.view = tableView_;
    • 是否选择了 'UITableView' 的子类?或者你拖动表格视图?请提供更多信息以解决问题。
    【解决方案2】:

    您可以手动完成:

    [_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionNone];
    
    CGRect rect = [_tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
    
    CGFloat offsetY = rect.origin.y+rect.size.height/2-_tableView.frame.size.height/2-_tableView.contentInset.top/2;
    if (offsetY < -_tableView.contentInset.top)
        offsetY = -_tableView.contentInset.top;
    else if (offsetY > _tableView.contentSize.height-_tableView.frame.size.height)
        offsetY = _tableView.contentSize.height-_tableView.frame.size.height;
    
    _tableView.contentOffset = CGPointMake(_tableView.contentOffset.x, offsetY);
    

    【讨论】:

      【解决方案3】:

      如果(单元格==nil)

      如果您在 CellforRowatIndexpath 中编写了这样的 if 语句,请删除该特定 if 语句并继续。那条线导致问题

      享受编码

      【讨论】:

        猜你喜欢
        • 2010-10-31
        • 2016-01-30
        • 2018-12-13
        • 2018-11-22
        • 1970-01-01
        • 2011-12-22
        • 2015-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多