2012-12-10 147 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大小。希望有所幫助!