具有自定义分页行为的UITableView [英] UITableView with custom paging-like behaviour

查看:97
本文介绍了具有自定义分页行为的UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UITableView中实现自定义大小页面的分页。
我想要实现的是让活动单元格的顶部与tableView的
顶部对齐,同时仍然显示tableView底部的下一个单元格的顶部(倾斜用户滚动并查看更多单元格。



我的单元格高度相等。



如果我设置 paging = YES 这会导致轻微的偏移量随着浏览页面而增加。这是因为我的tableView比单个单元格略高,单元格高度/页面大小没有对齐。



我尝试了不同的启用分页的东西。我尝试将tableView的大小设置为单元格的高度,但随后关闭剪裁和屏蔽,以便用户仍然可以看到下一个单元格。这不起作用,因为下一个单元格仅在单元格滚动到tableView的边界框之前的最后一个ms添加到底层scrollView。



然后我开始实现不同的scrollView委托方法来模仿分页行为 - 我似乎无法做到正确。



除此之外,我还尝试过这样的事情:

   - (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。



我在这里发现了一些试图达到同样功能的人的帖子,但答案却遭受同样的非本地快照感觉我尝试过的任何事情。



感谢您提供任何帮助。



iOS> = 5.0 <实现 scrollViewWillEndDragging:withVelocity:targetContentOffset:返回顶部坐标最接近 targetContentOffset 的单元格,最接近您的起始偏移量。 jjv360提供的解决方案做得很好,但你可能想稍微调整它,具体取决于你的单元平均有多高(jjv360的解决方案可能在真正的大单元上太过敏捷)。



我想补充一点,你可以让 UITableView 的减速更快,表现得更像页面 UIScrollView 通过更改其 decelerationRate 属性(只需在init / viewDidLoad / wherever中执行一次)。

  self.tableView.decelerationRate = UIScrollViewDecelerationRateFast; 


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).

My cells are all of equal height.

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.

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.

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));
}

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

解决方案

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).

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆