2016-12-28 96 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]; 
    } 
}