2016-12-28 68 views
1

我有一个的tableview在UITableViewCell包含一个文本框。当用户点击文本框并出现键盘时,在设置适当的内容插入后,滚动到所点击的行。滚动使用的UITableView沿着一个UIView滚动

现在,我有我需要做相应的调整表时滚动我的表视图的顶部显示一个UIView。

要做到这一点,我实现了如下scrollViewDidScroll:方法。虽然这在某些情况下有效,但在某些情况下它不起作用或给出随机结果。我在这里做错了什么?

- (void)scrollViewDidScroll:(UIScrollView *)iScrollView { 
    if (self.hollowView) { 
     CGPoint offset = iScrollView.contentOffset; 
     CGRect hollowViewFrame = self.hollowView.hollowFrame; 
     hollowViewFrame.origin.y += self.previousOffset - offset.y; 
     self.previousOffset = offset.y; 
     [self.hollowView resetToFrame:hollowViewFrame]; 
    } 
} 

- (void)keyboardDidHide:(NSNotification *)iNotification { 
    self.previousOffset = 0.0; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil]; 
} 
+1

尝试使用'IQKeyboardManager'管理键盘。它很容易使用 – kai

+0

如果您使用'autoLayout',您不应该通过操纵其框架来改变视图位置。 – Rikh

+0

如果您想要与视图一起滚动视图,为什么不使用tableViewheader。把你的视图放到tableViewHeader中,当视图滚动时,视图将沿着tableView滚动 –

回答

0

你可以在ios的滚动表中使用它。

-(void)keyboardWillShow:(NSNotification *)notification 
{ 

    NSDictionary* keyboardInfo = [notification userInfo]; 
    NSValue* keyboardFrameBegin =[keyboardInfovalueForKey:UIKeyboardFrameEndUserInfoKey]; 
    keyBoardHeight = keyboardFrameBegin.CGRectValue.size.height; 
    vwSendBottomSpaceContraint.constant = keyBoardHeight 
    [self performSelector:@selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1]; 

} 

-(void)keyboardWillHide:(NSNotification*)notification 
{ 

     vwSendBottomSpaceContraint.constant = 0; 
     [self performSelector:@selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1]; 

} 

-(void)scrollToBottomAnimated 
{ 

     NSInteger rows = [tableview numberOfRowsInSection:0]; 

     if(rows > 0) 
     { 
     [tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]       atScrollPosition:UITableViewScrollPositionBottomanimated:YES];  
     } 

} 
+0

不,我没有在键盘显示时寻找滚动表格。这我已经在做。我想要滚动的表格视图顶部还有一个空洞的视图。 – Abhinav

0

如果要同时滚动两个视图,那么你应该采取具有相同的尺寸和下面的方法只是设置了滚动内容偏移到他们俩的观点。这将切切实实的工作

enter code here - (void)scrollViewDidScroll:(UIScrollView *)iScrollView { 
if (self.scrollView) // Check for the scrolled view 
{ 
    // Set the Tableview offset directly 
    tableview.contentOffset = iScrollView.contentOffset 
} 
else { 
    //Set view scroll as per tableview scroll 
     view.contentOffset = iScrollView.contentOffset } } 

//注: - 确保看法应该是滚动型类型,或者观点心惊肉跳或显示黑屏

谢谢..

0

所以,这一块为我工作的人谁与相同的情况下奋力:

- (void)scrollViewDidScroll:(UIScrollView *)iScrollView { 
    if (self.hollowView) { 
     CGRect frame = [self.tableView rectForRowAtIndexPath:self.expandedCellIndexPath]; 
     frame.origin.y -= self.tableView.contentOffset.y; 
     [self.hollowView resetToFrame:frame]; 
    } 
}