我希望所选单元格始终位于 tableView
的最顶部位置。
我可以通过:
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
但我的问题是,例如,当单元格的数量仅为 3 时。当我选择单元格编号 3 时,它就停留在那里。这是正常行为吗?如果是,那么,你能提出一些建议,以便我实现我的目标吗?谢谢!
最佳答案
例如,您可以使用 tableview 的 contentInset
和 setContentOffset
属性
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 44.0f;//cell height
tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
//[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; //commenting u are setting it by using setContentOffset so dont use this
[tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:YES]; //set the selected cell to top
}
希望这对你有帮助.. :)
关于ios - 选择 Cell 时将 UITableViewCell 置于顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22497259/