UITableViewでのスクロール位置の設定
-
03-07-2019 - |
質問
iPhoneの連絡先アプリケーションの動作と似たような動作をするアプリケーションがあります。新しい連絡先ユーザーを追加すると、連絡先情報のある表示専用画面に移動します。 [すべての連絡先]を選択すると、ナビゲーションバーから、最近追加された連絡先が表示されているすべての連絡先のリストに移動します。
次を使用してビューを特定の行に移動できます
[itemsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
...しかし、機能していません。呼び出した直後にこれを呼び出しています:
[tableView reloadData];
ここで selectRowAtIndexPath:animated:scrollPosition
メソッドを呼び出すことは想定していません。しかし、ここにない場合、どこですか?
次のメソッドの後に呼び出されるデリゲートメソッドはありますか?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
解決
似たようなアプリがあると思います:アイテムのリスト。「+」をタップします->新しい画面に戻って、更新されたリストを確認し、スクロールして追加されたアイテムを下部に表示します。
要約すると、 viewWillAppear:animated:
に reloadData
を配置し、 viewDidAppear:animated:<に
scrollToRowAtIndexPath:...
を配置します/ code>。
// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (dataHasChanged) {
self.dataHasChanged = NO;
[[self tableView] reloadData];
} else {
self.scrollToLast = NO; // No reload -> no need to scroll!
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (scrollToLast) {
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
[[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
これが役立つことを願っています。リストの中央に何かを追加すると、代わりにその位置に簡単にスクロールできます。
他のヒント
これを試すことができます。uitableviewのスクロールボタンをクリックすると、私のアプリケーションが似たようなものになります。
[tableviewname setContentOffset:CGPointMake(0, ([arrayofcontact count]-10)*cellheight) animated:YES];
10は、上にスクロールするセルの数です
これがあなたを助けることを願っています:-)
viewDidAppear(_:)
からオフセットが設定されている場合、スクロールアニメーションは避けられません。初期スクロールオフセットを設定するより良い場所は、 viewWillAppear(_:)
です。コンテンツサイズはその時点では定義されていないため、Table Viewのレイアウトを強制する必要があります。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.setNeedsLayout()
tableView.layoutIfNeeded()
if let selectedIndexPath = selectedIndexPath, tableView.numberOfRows(inSection: selectedIndexPath.section) > 0 {
tableView.scrollToRow(at: selectedIndexPath, at: .top, animated: false)
}
}
スクロールをテストするのに十分なダミーの連絡先が追加されていますか? tableViewが特定のサイズである場合にのみ、iPhoneはスクロールする入力を見つけます。
これは私のプロジェクトで機能するコードです。前のボタンを使用するので、通常はUITABLEVIEWSCROLLPOSITIONを使用するために、Tableviewを少し下にスクロールします。 (前のセルを「見る」ことができない場合、前のボタンは機能しません。
カスタムメソッド呼び出しの一部を無視します。
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//Show the navButtons
[self navButtonAnimation];
DetailsStandardCell *cell = (DetailsStandardCell *)textField.superview.superview.superview;
self.parentController.lastCell = cell;
//Code to scroll the tableview to the previous indexpath so the previous button will work. NOTE previous button only works if its target table cell is visible.
NSUInteger row = cell.cellPath.row;
NSUInteger section = cell.cellPath.section;
NSIndexPath *previousIndexPath = nil;
if (cell.cellPath.row == 0 && cell.cellPath.section != 0) //take selection back to last row of previous section
{
NSUInteger previousIndex[] = {section -1, ([[self.sections objectForKey:[NSNumber numberWithInt:section - 1]]count] -1)};
previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];
}
else if (cell.cellPath.row != 0 && cell.cellPath.section != 0)
{
NSUInteger previousIndex[] = {section, row - 1};
previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];
}
[self.theTableView scrollToRowAtIndexPath: cell.cellPath.section == 0 ? cell.cellPath : previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}