2016-04-29 10 views
6

私はUITableViewCellにボタンを持っています。このボタンは、currentButtonと呼ばれるivarとして保存されています。このボタンをクリックすると、UIPickerViewとUIToolBarを含むUIViewが呼び出され、画面の下部に表示されます。しかし、私は何時間も他の投稿を見てきましたが、このコードはまだ動作しません。セルは時々キーボードの下にスクロールし、UITableViewの下部にあるセルはUIViewのbackgroundPickerViewの上に移動するのに十分なほどスクロールしません。これは私のコードです:UITableViewCellを目に見えるようにスクロールします。

CGPoint center = currentButton.center; 
    CGPoint rootViewPoint = [currentButton.superview convertPoint:center toView:self.view]; 
    NSIndexPath *indexPath = [measurementTableView indexPathForRowAtPoint:rootViewPoint]; 
    CGRect cellRect = [measurementTableView rectForRowAtIndexPath:indexPath]; 
    if (cellRect.origin.y + cellRect.size.height >= backgroundPickerView.frame.origin.y) { 
     [measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
     CGPoint offset = measurementTableView.contentOffset; 
     offset.y += ((cellRect.origin.y - cellRect.size.height) - backgroundPickerView.frame.origin.y); 
     [measurementTableView setContentOffset:offset animated:YES]; 
    } 

私はここで間違って何を見ていますか?

+0

self.viewとmeasurementTableViewは同じビューですか? – wj2061

+0

@ wj2061いいえ、self.viewはmeasurementTableViewを保持します –

+0

解決策は見つかりましたか? –

答えて

5

このコードは私にとって役に立ちました。ここに示唆されているものとは異なるアプローチを使用し、私が必要とするものには良いことです。

CGRect buttonFrame = [currentButton convertRect:currentButton.bounds toView:measurementTableView]; 
NSIndexPath *indexPath = [measurementTableView indexPathForRowAtPoint:buttonFrame.origin]; 
CGRect backPickFrame = [backgroundPickerView convertRect:backgroundPickerView.bounds toView:measurementTableView]; 
if (buttonFrame.origin.y + buttonFrame.size.height >= backPickFrame.origin.y) { 
    measurementTableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, backgroundPickerView.frame.size.height, 0.0); 
    [measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} else { 
    [measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
} 
5

UITableViewCellは再利用され、UITableViewCellのサブビューの参照は良好な方法ではありません。
特殊なUITableViewCellがUITableViewのvisibleCellsにない場合、そのフレームは未定義です。
解決策は以下のとおりです。

  1. はあなたが必要とするのと同じ構造を持つカスタムのUITableViewCellを作成します。

  2. カスタムUITableViewCellのindexpathの参照を保持します。

  3. このインデックスパスを使用して作業を行います。

私はこれが働くことを願っています:

[measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];  
CGPoint offset = measurementTableView.contentOffset;  
offset.y += backgroundPickerView.frame.height; 
3

まず問題はのUITableViewが部分的にキーボードの下に隠されていることです。したがって、下の行にスクロールしようとすると、テーブルが戻ってくるので、必要なコンテンツは表示されません。

キーボードの表示/非表示のときにこの問題を解決するには、2つの方法があります。アカウントのキーボードのサイズにテーブルの

  • 更新フレームを取るためにself.tableView.contentInstet.bottom

    • 更新値あなたは、自動レイアウトを使用している場合は、最善のアプローチは、(IMOこれは、より良いアプローチです)下の制約の一定の一部を更新となります。

    あなたはこれを行うことができますことをやった後:私は行うべきconvertionにビューを台無しに可能性が

    CGRect rectToBeVisible = [currentButton convertRect: currentButton.bounds 
                  toView: self.tableView]; 
    [self.tableView scrollRectToVisible: rectToBeVisible animated: YES]; 
    

    ので、少し実験が、一般的な概念を明確にする必要があります。

  • 関連する問題