2012-12-10 146 views
1

我不知道这是可能的,但这是我的情况:我有这个tableView有3个部分。如果您触摸accessoriesImage的第三部分中的白色星号,它会将该行插入收藏夹部分,就像accessories.sku一样。这工作得很好。停止从滚动tableView

现在,当您将行添加到收藏夹时,“收藏夹”部分将变大并将第三部分向下拖动到屏幕上。我知道在向第二部分添加行时显然需要这样做。但是,最后一部分有400多行,您可以非常快速地将屏幕顶部的两个部分滚动到屏幕上,这是我想停止滚动的地方。

会发生什么情况是,您向下滚动查看第3部分中的行,并且顶部两部分离开可见屏幕。然后,您触摸一行中的星号以添加新的最爱,这会触发将该行插入第2部分的收藏夹部分。但是,当它插入时,它也会将当前视图向下推下一行。我明白何时收藏夹部分是可见的,这是不可避免的,但是当该部分离开屏幕时,我不希望看到由于插入而导致我的行被压下。

下面的第二张图可能有助于解释更多。在这里,我们沿着屏幕向下滚动。如果我点击一个星星来添加一个收藏夹,它会在第2部分中插入一行,并将您在该图像中看到的行向下推动一行。我只是想在上面插入时保留这些行。

我很想听听你的想法。谢谢。

enter image description here

enter image description here

回答

3

首先,我会劝你考虑到没有显示在同一屏幕上的用户这么多的重复信息的设计 - 它会让你的应用更直观。例如,有一个切换所有非收藏行的选项。这样,您可以显示所有行并选择收藏夹,或者如果您只想从收藏夹中选择,可以隐藏它们。其次,如果您决定保留此设计,我建议您在插入新行时向下滚动表格视图,而不是试图阻止插入造成的滚动。对用户来说,这看起来就像没有发生过滚动一样。方法如下:

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大小。希望有所帮助!