まず、同じ画面でユーザーに表示される情報があまり繰り返されないデザインを検討することを強くお勧めします。これにより、アプリがより直感的になります。たとえば、お気に入りではないすべての行を切り替えるオプションを用意します。このようにして、すべての行を表示してお気に入りを選択するか、お気に入りからのみ選択する場合は非表示にすることができます。
第 2 に、このデザインを維持する場合は、挿入によるスクロールを停止しようとするのではなく、新しい行の挿入中にテーブル ビューを下にスクロールすることをお勧めします。ユーザーには、スクロールが発生していないように見えます。方法は次のとおりです。
UITableView には、CGPoint である ContentOffset プロパティがあります。このポイントの y プロパティは、テーブル ビューがどれだけ下にスクロールされるかを示す CGFloat です。したがって、コードで行を追加するときは、同時に画面も下にスクロールします。
// I use some variables for illustrative purposes here (you will need to provide them from your existing code):
// *** put your code to add or remove the row from favorites to your table view data source here ***
// start animation queue
[UIView beginAnimations:nil context:nil];
// check if a row is being added or deleted
if (isAddingRow) {
// if added, call to insert the row into the table view
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
// also tell the table view to scroll down the height of a row
[tableView setContentOffset:CGPointMake(tableView.contentOffset.x, tableView.contentOffset.y + kHeightOfRow) animated:YES];
} else {
// if deleted, call to delete the row into the table view
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deletedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
// also tell the table view to scroll down the height of a row
[tableView setContentOffset:CGPointMake(tableView.contentOffset.x, tableView.contentOffset.y - kHeightOfRow) animated:YES];
}
// launch animations
[UIView commitAnimations];
または、行が選択されているときにアニメーションを使用していない場合は、実際にアニメーションをオフにすることができます (アニメーションなしで上記と同じことを行います)。
// check if a row is being added or deleted
if (isAddingRow) {
// if added, call to insert the row into the table view
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
// also tell the table view to scroll down the height of a row
[tableView setContentOffset:CGPointMake(tableView.contentOffset.x, tableView.contentOffset.y + kHeightOfRow) animated:NO];
} else {
// if deleted, call to delete the row into the table view
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deletedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
// also tell the table view to scroll down the height of a row
[tableView setContentOffset:CGPointMake(tableView.contentOffset.x, tableView.contentOffset.y - kHeightOfRow) animated:NO];
}
また、テーブル ビューの contentSize が tableView のサイズよりも大きい (または大きくなる) 場合にのみ、setContentOffset:animated: メソッドを実行する必要があります。それが役立つことを願っています!