强制UITableView滚动到顶部? [英] Force UITableView to scroll to top?
问题描述
如果tableview包含少于10个左右的单元格,你会如何强制UITableViewCell滚动到顶部?我支持在tableview处于编辑模式时编辑tableView单元格中的托管对象上下文。不用说,如果一个单元格位于tableview的底部,当用户去编辑事件的标题或位置时,它会被键盘阻挡。我试过像这样的解决方案:
How would you go about forcing a UITableViewCell to scroll to the top if the tableview contains less than 10 or so cells? I support editing of Managed Object Contexts within my tableView cells while the tableview is in editing mode. Needless to say, if a cell is at the bottom of the tableview, it gets blocked by the keyboard when a user goes to edit the title or location of an event. I tried a solution like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if(_selectedIndex == indexPath.row){
_selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
return;
}
if(_selectedIndex >= 0){
NSIndexPath *previous = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
_selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previous]
withRowAnimation:UITableViewRowAnimationFade];
}
_selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView setContentOffset:CGPointMake(0, [tableView rowHeight]*indexPath.row) animated:YES];
}
但这并不会将tableview保留在contentOffset上。它将捕捉到顶部然后快照回来
But this does not keep the tableview at the contentOffset. it will snap to the top and then snap back
推荐答案
您是否考虑过滑动整个tableview?
Have you consider sliding the whole tableview up?
编辑我确实发现了一些看起来像你正在寻找的东西:
Code here 。
edit: I did find something that looks like it does what you're looking for: Code here.
此代码从底部缩小表格视图的插入,然后允许它显示其中一个较低的单元格,而不会向下捕捉。
This code shrinks the inset of the table view from the bottom, which then allows it display one of those lower cells without snapping back down.
这篇关于强制UITableView滚动到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!