2014-12-29 42 views
0

我有一个100行的表,并希望以编程方式滚动到任何元素,并选择该元素。在第一个控制器中,我指定了行和部分,并将带有UITableView的控制器推入导航堆栈。在“的tableView控制器” viewWillAppear我使用的代码:UITableView不能正常滚动到底部以编程方式

[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控制器”出现的tableView甚至预滚动到100个元素?

UPDATE。 我不使用任何xibs或情节串连图板,只是定义视图中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]; 

} 

回答

2

可能是你的看法高度不一样的tableview的。你的superview(你在其中添加你的tableview)应该是相同的。你的最后一行是隐藏的,因为你的视图不像你的tableview那么高,可能会解决你的问题。

+0

谢谢,有用一提,但我的tableview是控制器的看法: 'tableView_ = [[UITableView的页头] initWithFrame:方法self.view.bounds style:UITableViewStyleGrouped];' 'self.view = tableView_;' –

+0

有'UITableView'的选择子类?或者你拖动表视图?请提供更多信息以解决问题。 – Nitin

+0

请看更新 –

0

如果(细胞==零)

,如果你写的,如果像这样在你的cellForRowAtIndexPath语句中删除特定的if语句,然后继续。该行造成问题

享受编码

1

您可以手动做到这一点:

[_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); 
相关问题