問題描述
我正在嘗試在 UITableView 中為自定義大小的頁面實現分頁.我想要實現的是讓活動單元格的頂部與tableView 的頂部,同時仍顯示 tableView 底部的下一個單元格的頂部(以使用戶滾動并查看更多單元格).
I am trying to implement paging for custom sized pages in the UITableView. What I am trying to achieve is to have the top of the active cell align with the top of the tableView, whilst still showing the top of the next cell at the bottom of the tableView (to incline the user to scroll and see more cells).
我的單元格都等高.
如果我設置 paging=YES
這會導致輕微的偏移量隨著我翻閱頁面而增加.這是由于我的 tableView 比單個單元格略高,并且單元格高度/頁面大小未對齊.
If I set paging=YES
this results in a slight offset that increases as I flick through the pages. This is due to my tableView being slightly taller than a single cell and the cell height/page size not aligning.
我在啟用分頁的情況下嘗試了不同的方法.我嘗試將 tableView 的大小設置為單元格的高度,然后關閉剪輯和遮罩,以便用戶仍然可以看到下一個單元格.這不起作用,因為下一個單元格僅在單元格滾動到 tableView 的邊界框之前的最后一毫秒添加到底層滾動視圖.
I have tried different things with paging enabled. I tried setting the size of the tableView to the height of the cell, but then turning off clipping and masking so the user could still see the next cell. This does not work as the next cell is only added to the underlying scrollView at the very last ms before the cell scrolls into the bounding box of the tableView.
然后我開始實現不同的 scrollView 委托方法來模擬分頁行為 - 我似乎無法做到這一點.
I then started implementing the different scrollView delegate methods to mimic the paging behaviour - I can not seem to get it right.
除其他外,我嘗試過這樣的事情:
I have, among other things, tried something like this:
- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
float cellHeight = [myCell rowHeight];
int index = floorf(scrollView.contentOffset.y / cellHeight);
*targetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y = (index * cellHeight));
}
雖然它做了一些正確的事情,但它的行為與啟用分頁的 scrollView/tableView 完全不同.
While it sort of does the right thing, it behaves nothing like a scrollView/tableView with paging enabled.
我在這里找到了一些試圖實現相同目標的人的帖子,但答案與我自己嘗試過的任何事情一樣具有非本地快照感覺".
I have found a few posts here from people trying to achieve the same thing, but the answers suffer from the same "non-native-snap-feel" that anything I tried myself does.
感謝您提供的任何幫助.
Thanks for any help given.
iOS >= 5.0
推薦答案
實現 scrollViewWillEndDragging:withVelocity:targetContentOffset:
以返回距離 targetContentOffset
最近的單元格的頂部坐標,而不是最接近您的起始偏移量.jjv360 提供的解決方案可以很好地做到這一點,但您可能需要根據單元格的平均高度對其進行一些調整(jjv360 的解決方案可能對非常大的單元格過于活潑).
Implement scrollViewWillEndDragging:withVelocity:targetContentOffset:
to return top coordinate of the cell closest to targetContentOffset
, not closest to your starting offset. The solution jjv360 provided does that well, but you might want to tweak it a bit depending on how high your cells are on average (jjv360's solution might be too snappy on really big cells).
我想補充一點,您可以通過更改其 decelerationRate
屬性來使 UITableView
的減速更快,使其表現得更像分頁 UIScrollView
(只需在 init/viewDidLoad/wherever 中執行一次).
I would just like to add that you can make UITableView
's deceleration faster to behave more like paged UIScrollView
by changing its decelerationRate
property (just do it once in init/viewDidLoad/wherever).
self.tableView.decelerationRate = UIScrollViewDecelerationRateFast;
這篇關于具有自定義分頁行為的 UITableView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!