无限滚动的TableView
来源:互联网 发布:民族知识知多少 编辑:程序博客网 时间:2024/05/16 16:20
转自 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.
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
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
- 无限滚动的TableView
- Unity3D-UGUI无限滚动加载TableView列表
- 横向滚动的tableview
- 无限滚动的轮播图
- IOS tableview 的滚动效果
- 图片新闻的无限滚动
- UI06_无限滚动的相册
- 无限滚动
- 如何设置ios tableView的section跟着tableView一起滚动
- 优化tableview cell的滚动速度心得
- 利用DTGridView实现横向滚动的tableview
- 如何提高tableView的滚动速度
- 利用DTGridView实现横向滚动的tableview
- 利用DTGridView实现横向滚动的tableview
- tableView 设置可以滚动的背景图
- 关于tableView滚动scroll的一些知识
- tableview滚动实时调用的方法
- TableView滚动时的懒加载
- BZOJ1003: [ZJOI2006]物流运输
- 如何同时向两张表插入记录
- 图像透明度
- win10 uwp 获取按钮鼠标左键按下
- Vue.js vs React vs Angular 深度对比
- 无限滚动的TableView
- 微服务实战:从架构到发布(一)
- 17个你必须牢记的Win7快捷键
- 人脸检测与人脸对齐指标
- Fragment的陷阱
- C#怎么实现文件下载功能
- MyBatis(6):MyBatis 集成 Spring 事务管理(下)
- "/"程序中的服务器错误
- easyui-datagrid 编辑模式详解