简体   繁体   中英

UITableView scrollToRowAtIndexPath doesn't scroll to the first row if it only has one item

I've implemented a tableView on the iPhone with the ability to search using the searchBar. I'm trying to mimic the effect that hides the searchBar in my tableView when it first loaded.

The behavior is expected when the tableView contains more than one row. However, it doesn't scroll to the first row when there is only one row. It shows the searchBar.

- (void)viewDidLoad {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
                          atScrollPosition:UITableViewScrollPositionTop
                                  animated:NO];
}

其实我不了解所有事情(将表格中的单元格添加到表中时,可能是出问题了-搜索栏在哪里,在什么单元格中?),但是您可以尝试

[self.tableView scrollRectToVisible:CGRectMake(0, yValue, 10, 10) animated:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM