首先,我会劝你考虑到没有显示在同一屏幕上的用户这么多的重复信息的设计 - 它会让你的应用更直观。例如,有一个切换所有非收藏行的选项。这样,您可以显示所有行并选择收藏夹,或者如果您只想从收藏夹中选择,可以隐藏它们。其次,如果您决定保留此设计,我建议您在插入新行时向下滚动表格视图,而不是试图阻止插入造成的滚动。对用户来说,这看起来就像没有发生过滚动一样。方法如下:
UITableView有一个ContentOffset属性,它是一个CGPoint。此点的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];
}
您也想进行setContentOffset:动画:方法只有当表视图contentSize是(或即将)比的tableView大小。希望有所帮助!