我有一个100行的表,并希望以编程方式滚动到任何元素,并选择该元素。在第一个控制器中,我指定了行和部分,并将带有UITableView的控制器推入导航堆栈。在“的tableView控制器” viewWillAppear
我使用的代码:UITableView不能正常滚动到底部以编程方式
[tableView_ selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
好奇的部分是,它完美地滚动到任意的99个第一行,100个行被选中,但仍“下方”的可见区域。我必须手动滚动表格才能显示它。
如果我将该代码放置在viewDidAppear
中,表格会很好地滚动,但会显着延迟。
什么我也尝试:
- 减少的tableView的高度。我得到了部分覆盖屏幕的tableView,最后一行被遮盖了。
- 使用contentSize - 将其高度设置为较小的大值 - 没有运气。
- 设置
contentInset.bottom
- 工作,但我有一个无用的空间在底部。 在
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];
}
谢谢,有用一提,但我的tableview是控制器的看法: 'tableView_ = [[UITableView的页头] initWithFrame:方法self.view.bounds style:UITableViewStyleGrouped];' 'self.view = tableView_;' –
有'UITableView'的选择子类?或者你拖动表视图?请提供更多信息以解决问题。 – Nitin
请看更新 –