无限滚动的TableView
2016-12-29 09:37
176 查看
转自 https://iphone2020.wordpress.com/2012/10/01/uitableview-tricks-part-2-infinite-scrolling/, 感谢作者!
Last time I posted about the UITableView Tricks to lay
the cells of the tablview in Circular fashion. It was well received by the community and thank you all for your feedback and compliments.
I received many requests for making this circular list scroll infinitely as with UIPickerView, which means that the list repeats forever. It really makes sense just because the list itself is in circular fashion and user expect the content to repeat, without
him/her having to scroll back to top.
I came across the 2011 WWDC video – Advanced Scrollview Techniques which provides the best feasible solution to support infinite scrolling. Many attempts have been made before to find a solution by having the height of the scrollview’s content
size to a large number so that it takes days, if not AGES for a user to get to the end point. But it was never a complete solution.
I went a bit closer to the perfect solution and here it is. The goal here is to provide a generic solution to a UITableView while having the user/ developer to do minimum or ZERO changes. This lead me to create a subclass of UITableView, and named as BBTableView
By using BBTableView, a developer has to do ZERO changes ( some minor changes for additional functionality, explained later ) to enable infinite scrolling.
Behind the scenes:
The core logic of this solution is
1. To increase the tableview content by a factor of 3, so that we make the 3 copies of the content laid one after another vertically.
2. Whenever the top end of the scroll is reached, move the the scroll offset back to start of the 2nd copy
3. When the bottom end of the scroll is reached, we move the scroll offset back to the start of the 2nd copy minus the height of the tableview, so that we end up showing the same content as we are now.
![](https://iphone2020.files.wordpress.com/2012/10/infinite_scrollinginfinite-scrolling.jpg?w=1200&h=604)
This solution did bring an obstacle to my goal of making this component as a Drag-n-Drop component. i.e. Making 2 additional copies of rows provided by datasource also changes the index paths used in datasource or delegate methods that are propagated to the
component user, a developer who would use this component.
This can be avoided by intercepting the delegate or datasource methods to morph the index paths exchanged between the UITableView and the its datasource or delegate methods.
Thanks Evadne Wu , for allowing me to use the interceptor component written by her.
Two properties have been added to BBTableView,
a. contentAlignment
Allows the change the direction of the semi-circle. It supports two directions
![](https://iphone2020.files.wordpress.com/2012/12/leftclockwise.png?w=554&h=1042)
![](https://iphone2020.files.wordpress.com/2012/12/leftscreen-shot-2012-12-07-at-9-25-33-pm.png?w=554&h=1042)
eBBTableViewContentAlignmentRight – Layouts the circle Clockwise
eBBTableViewContentAlignmentRight – Layouts the circle Anti-Clockwise
b. enableInfiniteScrolling
set YES to enable infinite scrolling. NO to reset to default scrolling behavior of UITableView
As every software, this component too have its Cons:
1. Since the issue with Index Paths resolved by intercepting delegate / datasource methods, Any additions or modifications to UITableView datasource/ delegate methods involving index path, requires a change to counterpart datasource / delegate
methods implemented in BBTableView. However, on a good note, this change includes only getting the morphed index path and calling appropriate method on the receiver of Datasource interceptor.
A sample intercepted Datasource method would look like:
– (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [_dataSourceInterceptor.receiver tableView:tableViewcellForRowAtIndexPath:MORPHED_INDEX_PATH(indexPath)];
}
Check the source code updated in github here https://github.com/bharath2020/UITableViewTricks
Last time I posted about the UITableView Tricks to lay
the cells of the tablview in Circular fashion. It was well received by the community and thank you all for your feedback and compliments.
I received many requests for making this circular list scroll infinitely as with UIPickerView, which means that the list repeats forever. It really makes sense just because the list itself is in circular fashion and user expect the content to repeat, without
him/her having to scroll back to top.
I came across the 2011 WWDC video – Advanced Scrollview Techniques which provides the best feasible solution to support infinite scrolling. Many attempts have been made before to find a solution by having the height of the scrollview’s content
size to a large number so that it takes days, if not AGES for a user to get to the end point. But it was never a complete solution.
I went a bit closer to the perfect solution and here it is. The goal here is to provide a generic solution to a UITableView while having the user/ developer to do minimum or ZERO changes. This lead me to create a subclass of UITableView, and named as BBTableView
By using BBTableView, a developer has to do ZERO changes ( some minor changes for additional functionality, explained later ) to enable infinite scrolling.
Behind the scenes:
The core logic of this solution is
1. To increase the tableview content by a factor of 3, so that we make the 3 copies of the content laid one after another vertically.
2. Whenever the top end of the scroll is reached, move the the scroll offset back to start of the 2nd copy
3. When the bottom end of the scroll is reached, we move the scroll offset back to the start of the 2nd copy minus the height of the tableview, so that we end up showing the same content as we are now.
![](https://iphone2020.files.wordpress.com/2012/10/infinite_scrollinginfinite-scrolling.jpg?w=1200&h=604)
This solution did bring an obstacle to my goal of making this component as a Drag-n-Drop component. i.e. Making 2 additional copies of rows provided by datasource also changes the index paths used in datasource or delegate methods that are propagated to the
component user, a developer who would use this component.
This can be avoided by intercepting the delegate or datasource methods to morph the index paths exchanged between the UITableView and the its datasource or delegate methods.
Thanks Evadne Wu , for allowing me to use the interceptor component written by her.
Two properties have been added to BBTableView,
a. contentAlignment
Allows the change the direction of the semi-circle. It supports two directions
![](https://iphone2020.files.wordpress.com/2012/12/leftclockwise.png?w=554&h=1042)
![](https://iphone2020.files.wordpress.com/2012/12/leftscreen-shot-2012-12-07-at-9-25-33-pm.png?w=554&h=1042)
eBBTableViewContentAlignmentRight – Layouts the circle Clockwise
eBBTableViewContentAlignmentRight – Layouts the circle Anti-Clockwise
b. enableInfiniteScrolling
set YES to enable infinite scrolling. NO to reset to default scrolling behavior of UITableView
As every software, this component too have its Cons:
1. Since the issue with Index Paths resolved by intercepting delegate / datasource methods, Any additions or modifications to UITableView datasource/ delegate methods involving index path, requires a change to counterpart datasource / delegate
methods implemented in BBTableView. However, on a good note, this change includes only getting the morphed index path and calling appropriate method on the receiver of Datasource interceptor.
A sample intercepted Datasource method would look like:
– (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [_dataSourceInterceptor.receiver tableView:tableViewcellForRowAtIndexPath:MORPHED_INDEX_PATH(indexPath)];
}
Check the source code updated in github here https://github.com/bharath2020/UITableViewTricks
相关文章推荐
- cocos2d-x无限滚动背景
- 无限滚动相册切换swift版
- iOS之scrollView分页无限滚动框架
- LoopBar – Tap酒吧与无限滚动
- vue2 无限滚动图片加文字
- 【Cocos2d-x】新手自学(九)滚动背景..实现无限卷轴效果(2.0.1版本)
- Cocos2dx中利用双向链表实现无限循环滚动层
- iOScollectionView广告无限滚动实例(Swift实现)
- 记android ViewPager实现轮播图的无限滚动笔记
- 网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片
- Android ViewPager无限循环滑动并可自动滚动完整实例
- 无限滚动插件infinite-scroll介绍
- Cocos2d-X 3.4版-地图无限滚动与边缘检测《赵云要格斗》
- 无限滚动HTML UL结构
- 使用MJRefresh的情况下如何让tableView滚动到顶部
- 基于jQuery的公告无限循环滚动实现代码
- 轮播图无限滚动
- iOS 简易无限滚动的图片轮播器-SDCycleScrollView
- 利用UIScrollView实现展示图片的无限滚动及自动滚动
- js滚动加载(无限加载)