Objective c UITableView设置内容偏移量,但不';滚动表格视图?
我在Objective c UITableView设置内容偏移量,但不';滚动表格视图?,objective-c,ios,cocoa-touch,uitableview,Objective C,Ios,Cocoa Touch,Uitableview,我在UITableView上使用setContentOffset,因为我想首先隐藏一个搜索字段,它是我的tableHeaderView [self.tableView setContentOffset:CGPointMake(0, 56)]; // No scroll please! 每次按下新的viewController时,我都想用contentOffset隐藏搜索栏。但当我弹出一个viewController时,由于某种原因,该偏移不再有效,并显示搜索栏。为什么会这样?您可以尝试在以下方
UITableView
上使用setContentOffset
,因为我想首先隐藏一个搜索字段,它是我的tableHeaderView
[self.tableView setContentOffset:CGPointMake(0, 56)]; // No scroll please!
每次按下新的
viewController
时,我都想用contentOffset隐藏搜索栏。但当我弹出一个viewController
时,由于某种原因,该偏移不再有效,并显示搜索栏。为什么会这样?您可以尝试在以下方面实现它
- (void)viewWillAppear:(BOOL)animated {
[self.tableView setContentOffset:CGPointMake(0, 56)];
}
这将使表格在显示在屏幕上之前处于正确的位置,我假设您的意思是在设置位置时没有动画。我猜您想阻止用户滚动到屏幕顶部。如果是这样,您可以实现以下UITableView委托(在iOS5及更高版本上):
ScrollViewWillendDraging:withVelocity:targetContentOffset:
这允许您修改contentOffset中更改的最终目标。在实施过程中,您需要:
- (void)scrollViewWillEndDragging:(UIScrollView *)theScrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if(targetContentOffset->y < 56) {
targetContentOffset->y=56;
}
}
-(void)ScrollView将结束拖动:(UIScrollView*)带速度的ScrollView:(CGPoint)速度目标内容偏移量:(inout CGPoint*)目标内容偏移量
{
如果(targetContentOffset->y<56){
targetContentOffset->y=56;
}
}
如果你试图在失去某个东西的行为中保持它的价值,自然的解决办法是自己抓住它(“保持/恢复”):
在另一种情况下,可能需要按住您所在的行,然后滚动回该行:
(改编自:) (Swift)
1.保留当前行
我不明白这个问题,setContentOffset的目的是设置滚动位置。我的意思是,每次我按下一个新的
viewController
时,我都想用contentOffset隐藏搜索栏。但当我弹出一个viewController
时,由于某种原因,该偏移不再有效。这是为什么?我想@nizz的答案会起作用。每次显示视图时重置contentOffset。这会起作用,但在VC1中,他们会滚动到底部,然后通过触摸单元格来推送新的VC。然后,当它们弹回来时,如果我调用setContentOffset
,它会将tableView滚动到顶部,即使它确实应该在用户拥有它的地方保持滚动。
let holdIndexPath = tableView.indexPathForSelectedRow()
// The next line is to make sure the row object exists.
tableView.reloadRowsAtIndexPaths([holdIndexPath], withRowAnimation: .None)
tableView.scrollToRowAtIndexPath(holdIndexPath, atScrollPosition: atScrollPosition, animated: true)