Coder Social home page Coder Social logo

alibaba / lazyscrollview Goto Github PK

View Code? Open in Web Editor NEW
1.8K 49.0 285.0 162 KB

An iOS ScrollView to resolve the problem of reusability in views.

License: MIT License

Ruby 2.62% Objective-C 95.22% Python 2.16%
lazyscrollview ios reusability scrollview tangram

lazyscrollview's Introduction

CocoaPods CocoaPods CocoaPods

LazyScrollView

中文说明

基于 LazyScrollView,我们创造了一个动态创建模块化 UI 页面的解决方案,详情可见 Tangram-iOS

LazyScrollView is an iOS ScrollView, to resolve the problem of reusability of views.

Comparing to UITableView, LazyScrollView can easily create different layout, instead of the single row flow layout.

Comparing to UICollectionView, LazyScrollView can create views without Grid layout, and provides a easier way to create different kinds of layous in a ScrollView.

We create a modular UI solution for building UI page dynamically based on LazyScrollView, you can see more info from this repo: Tangram-iOS

Installation

LazyScroll is available as LazyScroll in CocoaPods.

pod 'LazyScroll'

You can also download the source files from release page and add them into your project manually.

Usage

#import "TMMuiLazyScrollView.h"

Then, create LazyScrollView:

TMMuiLazyScrollView *scrollview = [[TMMuiLazyScrollView alloc]init];
scrollview.frame = self.view.bounds;

Next, implement TMMuiLazyScrollViewDataSource:

@protocol TMMuiLazyScrollViewDataSource <NSObject>

@required

// Number of items in scrollView.
- (NSUInteger)numberOfItemInScrollView:(TMMuiLazyScrollView *)scrollView;

// Return the view model (TMMuiRectModel) by index.
- (TMMuiRectModel *)scrollView:(TMMuiLazyScrollView *)scrollView rectModelAtIndex:(NSUInteger)index;

// Return view by the unique string that identify a model (muiID).
// You should render the item view here.
// You should ALWAYS try to reuse views by setting each view's reuseIdentifier.
- (UIView *)scrollView:(TMMuiLazyScrollView *)scrollView itemByMuiID:(NSString *)muiID;

@end

Next, set datasource of LazyScrollView:

scrollview.dataSource = self;

Finally, do reload:

[scrollview reloadData];

For more details, please clone the repo and open the demo project.

lazyscrollview's People

Contributors

fydx avatar harrisonxi avatar longerian avatar mikeafc avatar wanhmr avatar zhongwuzw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lazyscrollview's Issues

dequeueReusableItemWithIdentifier: 方法需要改进

假如我有两个 UIView 的子类:A 和 B,我要在某个 rect 内需要填充一个 View,可能是 A 也可能是 B,取决于业务条件,类似的代码可能如下所示:

UIView *view = nil;
if (some_condition) {
    view = (A *)[scrollView dequeueReusableItemWithIdentifier:reuseIdentifier_A];
    if (view == nil) {
        // create a view
        view = [A new];
        view.reuseIdentifier = reuseIdentifier_A;
    }
} else {
    view = (B *)[scrollView dequeueReusableItemWithIdentifier:reuseIdentifier_B];
    if (view == nil) {
        // create a view
        view = [B new];
        view.reuseIdentifier = reuseIdentifier_B;
    }
}

return view;

然后我在第二次调用reloadData方法时,数据源数据已发生变化,通过dequeueReusableItemWithIdentifier:方法可能会获取到错误类型的 view 实例,比如当some_condition 条件为真时,dequeueReusableItemWithIdentifier:方法获取的 view 类型可能为 B。

感觉delegate实现不太好

重写了UIScrollView的setDelegate方法,内部用了一个变量去记录外部对delegate的设置,然后实现了所有UIScrollView的delegate方法,再对外分发delegate事件,这样实现感觉不好,写了很多没有用的代码,如果系统新版增加了一个delegate方法,岂不是要跟着更新增加这样一个方法?

- (UIView *)scrollView:(TMMuiLazyScrollView *)scrollView itemByMuiID:(NSString *)muiID 代理返回view时,

  • (UIView *)scrollView:(TMMuiLazyScrollView *)scrollView itemByMuiID:(NSString *)muiID
    {
    //Find view that is reuseable first.
    LazyScrollViewCustomView *label = (LazyScrollViewCustomView *)[scrollView dequeueReusableItemWithIdentifier:@"testView"];
    // 省略代码。。。
    [scrollView addSubview:label];
    return label;
    }
    其它代码省略了,现在需要代理主动调用[scrollView addSubview:label];,才能把view添加到scrollview山去,是不是可以设计的跟UITableView一样,添加子视图的操作由scrollview自己来操作,代理实现者只要返回view即可,不用自己主动去把view添加到scrollview上去

最新更新的疑问

大致的看了一下,之前我也遇到过用Lazyscroll套在另外一个scrollview里面,外面的需要滑动,所以LazyScroll的高度需要固定,但是这样lazy loading就无效了,所以这个outerScrollView是用来解决这个问题的么

LazyScrollView的subView起点坐标问题

子视图的起点坐标为何不是从左上角开始算的?如下self.lazyScroll是LazyScrollView,因为设置过setTranslucent和edgesForExtendedLayout,它的起点坐标是从从导航栏左上角开始计算的,但是加在LazyScrollView上的子视图的起点坐标,为何不是从LazyScrollView的左上角开始计算的?

// view
[self.view addSubview:self.lazyScroll];

// frame
[self.lazyScroll mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.right.mas_equalTo(self.view);
    make.bottom.mas_equalTo(self.view.mas_bottom);

}];

 // Here is frame array for subView.
 // LazyScrollView must know item view's frame before rending.
 CGFloat maxY = 0, currentY = 0 - APP_NavHeight;
 CGFloat viewWidth = CGRectGetWidth(self.view.bounds);
 [self addRect:CGRectMake(0, 0 + currentY, viewWidth , 200) andUpdateMaxY:&maxY];

复用的UITextView无法选取文字点击链接了

LazyScrollViewdequeueReusableItem找到可以reuse的UITextView以后,load进新的NSAttributedString, 只有styling还保持,但是user interactive相关的功能都失效了,比如说文字选取

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.