美文网首页
使用UICollectViewController,SDWebI

使用UICollectViewController,SDWebI

作者: 动感新势力fan | 来源:发表于2016-03-13 18:55 被阅读149次

1.先上效果图

gg.gif

2.先用青花瓷软件获取一个app的post请求链接
3.使用AFNetWorking实现对Http请求的封装类 HttpTool

//这个方法可以post登录
+(void)Post:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 创建请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    [mgr POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

parameters 为post请求body的参数 为oc对象
以下两张写法均可
NSDictionary *params = @{@"password" : @"1", @"username" : @"cr"};
NSDictionary *params = @{@"password" : @1, @"username" : @"cr"};

4.构造一个 StatusParam 表明post请求的参数模型对象

@property (nonatomic, copy) NSString *app;
@property (nonatomic, copy) NSString *backup;
@property (nonatomic, copy) NSString *client_id;
@property (nonatomic, copy) NSString *cver;
@property (nonatomic, copy) NSString *device_id;
@property (nonatomic, copy) NSString *device_token;
@property (nonatomic, copy) NSString *device_version;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *idfa;
@property (nonatomic, copy) NSString *open_udid;
/**
 *  若指定此参数,则返回对应的数据,一次数据有10条
 */
@property (nonatomic, copy) NSString *p;
@property (nonatomic, copy) NSString *qudaoid;
@property (nonatomic, copy) NSString *size;
@property (nonatomic, copy) NSString *uuid;
@property (nonatomic, copy) NSString *ver;
@property (nonatomic, copy) NSString *via;

5.创建StatusResult类 对返回的数据进行处理
可能json有好几层 有用的数据在里面的层

-(instancetype)initWithArr:(id)array
{
    if(self = [super init])
    {
        NSDictionary *data = [array objectForKey:@"data"];
        
        
        NSArray *dicArray = [data objectForKey:@"list"];
        
            //字典转模型
            NSMutableArray *tmpArray = [NSMutableArray array];
            for (NSDictionary *dic in dicArray) {
                CZStatus *status = [[CZStatus alloc]initWithDic:dic];
                [tmpArray addObject:status];
            }
          _statuses = tmpArray;
       
    }
    return self;
}

6.创建StatusTool工具类 block回调HttpTool基础类,根据不同Param和url获取不同的json反序列数据(AFN已经实现了反序列化的功能)

+ (void)newStatusWithSinceId:(NSString *)p success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure
{
    // 创建参数模型
    CZStatusParam *param = [[CZStatusParam alloc] init];
    if (p) { // 有数据,才需要下拉刷新
        param.p = p;
        
    }
    
    [CZHttpTool Post:@"http://v.higo.meilishuo.com/shop/get_more" parameters:param.keyValues success:^(id responseObject) {
        
        CZStatusResult *result = [[CZStatusResult alloc] initWithArr:responseObject];
        if(success)
        {
            success(result.statuses);
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

7.创建 自定义UICollectViewControllercell 的Status模型类 .h中的属性最好和获取来的数据属性的名字相同


-(instancetype)initWithDic:(NSDictionary*)dic
{
    if(self = [super init]){
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
+(instancetype)StatusWithDic:(NSDictionary*)dic
{
    return [[self alloc] initWithDic:dic];
}

8.创建HighGoCollectionViewCell类 使用storyboard方式加载
使用storyboard方式加载 的话有两个注意点
1.storyboard中cell的reuseIdentifier必须和代码里的一致
2.代码默认生成的注册要注释掉

//    [self.collectionView registerClass:[HighGoCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

重写HighGoCollectionViewCell中的data属性的set方法,减耦合

-(void) setData:(CZStatus *)data
{
    _data = data;
    
    //设置头像的圆角    暂时用这种,这种性能不高
    _group_header.layer.masksToBounds =YES;
    [_group_header.layer setCornerRadius:75];
    _group_header.layer.rasterizationScale=[UIScreen mainScreen].scale;
    
   [_group_header sd_setImageWithURL:[NSURL URLWithString:_data.group_header] placeholderImage:[UIImage imageNamed:@"avatar1"]];
    
    _group_name.text = _data.group_name;//名称
    _city.text = _data.city;//城市
    _group_desc.text = _data.group_desc;//具体描述
}

9.最后一步,使用UICollectViewController 创建HighgoCollectionViewController类

首次加载,自动请求一次
上拉刷新请求一次

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    // Register cell classes
//    [self.collectionView registerClass:[HighGoCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self loadNewStatus];
    // 添加上拉刷新控件
    [self.collectionView addFooterWithTarget:self action:@selector(loadNewStatus)];
}

请求一次数据,一次的数据量为10个

- (void)loadNewStatus
{
    NSString *sinceId = @"1";
    if (self.status.count) { // 有数据,才需要下拉刷新
        
        sinceId = [NSString stringWithFormat:@"%lu",1+self.status.count/10];
    }
    
    [CZStatusTool newStatusWithSinceId:sinceId success:^(NSArray *statuses) { // 请求成功的Block
        
        // 结束下拉刷新
        [self.collectionView footerEndRefreshing];
        
        // 模型转换视图模型 CZStatus -> CZStatusFrame
        NSMutableArray *statusA = [NSMutableArray array];
        for (CZStatus *a in statuses) { 
            [statusA addObject:a];
        }
        [self.status addObjectsFromArray:statusA];
        
        // 刷新表格
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

数据源代理方法

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

     return self.status.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    HighGoCollectionViewCell *cell = (HighGoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
    CZStatus *status = self.status[indexPath.row];
    
    cell.data = status;   
    return cell;
}

如果要自定义frame 使用mvvm


使用UICollectViewController,SDWebImage,AFNetWorking实现HighGo购物界面

如果还有哪些方面不足,请大家指教

相关文章

网友评论

      本文标题:使用UICollectViewController,SDWebI

      本文链接:https://www.haomeiwen.com/subject/pgxflttx.html