滚动UIView和UITableView滚动

时间:2016-12-28 09:22:11

标签: ios objective-c iphone uitableview uiview

我有一个tableview,其中UITableViewCell包含一个文本字段。当用户点击文本字段并出现键盘时,我会在设置正确的内容插入后滚动到点击的行。

现在,我的桌面视图顶部显示了一个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];
}

4 个答案:

答案 0 :(得分: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];       
       }

}

答案 1 :(得分:0)

如果要同时滚动两个视图,则必须同时拍摄两个具有相同大小的视图,并且在以下方法中,只需将scrollview内容偏移设置为它们。它会勉强工作

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   } }

//注意: - 确保视图应该是滚动视图类型,或者视图是否被吓到或显示黑屏

谢谢..

答案 2 :(得分:0)

使用此库:

https://github.com/hackiftekhar/IQKeyboardManager

在文本字段上点击时,它将处理视图的位置。

答案 3 :(得分: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];
    }
}