瀑布流教程3

接教程2,接下来实现最后一步。用ViewController把拿到的图片显示在瀑布流上。

在这里,用到代理协议,和数据协议。


//.h文件

#import <UIKit/UIKit.h>

#import "WaterFlowView.h"

#import "ImageViewCell.h"

                                        //瀑布流(UIscrollview)代理协议,瀑布流(UIscrollview)数据协议,

@interface CenterViewController :UIViewController<WaterFlowViewDelegate,WaterFlowViewDataSource>

{

    

    NSMutableArray *arrayData;

    WaterFlowView *waterFlow;

}


- (void)dataSourceDidLoad;

- (void)dataSourceDidError;


@end



//.m文件

#import "CenterViewController.h"

#import "NSObject+SBJson.h"

@implementation CenterViewController


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    

}

#pragma mark - View lifecycle


- (void)loadInternetData {

    // Request

    NSString *URLPath = [NSStringstringWithFormat:@"http://manhuazhuocom.h02.000pc.net/ComicService/ComicHandle.ashx?method=recommend&pageindex=0&pagesize=60"];      //我用到的是“爱漫画”网站

    NSURL *URL = [NSURLURLWithString:URLPath];    //字符串转URL

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:URL];     //发送数据请求

    

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) {

        

        NSInteger responseCode = [(NSHTTPURLResponse *)responsestatusCode];

        if (!error && responseCode == 200) {


            NSStringEncoding enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);

            NSString *ss=[[NSStringalloc]initWithData:dataencoding:enc];  //字符编码


                NSMutableArray*  arr_aa=[ssJSONValue]; //json解析

                for(NSMutableDictionary *dic1in arr_aa)

                {

                    [arrayData addObject:dic1];

                }

                [self dataSourceDidLoad];

        }

    }];

}

- (void)dataSourceDidLoad {

    [waterFlowreloadData];

}


- (void)dataSourceDidError {

    [waterFlowreloadData];

}



- (void)viewDidLoad

{

    [superviewDidLoad];

    arrayData = [[NSMutableArrayalloc]init];

    waterFlow = [[WaterFlowViewalloc]initWithFrame:CGRectMake(0,0,320,460-44)];

    waterFlow.waterFlowViewDelegate =self;

    waterFlow.waterFlowViewDatasource =self;

    waterFlow.backgroundColor = [UIColorwhiteColor];

    [self.view addSubview:waterFlow];

    [waterFlow release];

    [selfloadInternetData];

}


-(void)loadMore{


    [waterFlowreloadData];

}


#pragma mark WaterFlowViewDataSource

- (NSInteger)numberOfColumsInWaterFlowView:(WaterFlowView *)waterFlowView{


    return3;  //直接让它返回3

}


- (NSInteger)numberOfAllWaterFlowView:(WaterFlowView *)waterFlowView{


    return [arrayDatacount];

}


- (UIView *)waterFlowView:(WaterFlowView *)waterFlowView cellForRowAtIndexPath:(IndexPath *)indexPath{

    

    ImageViewCell *view = [[ImageViewCellalloc]initWithIdentifier:nil];

    

    return view;

}


-(void)waterFlowView:(WaterFlowView *)waterFlowView  relayoutCellSubview:(UIView *)view withIndexPath:(IndexPath *)indexPath{

    //arrIndex是某个数据在总数组中的索引,在ImageViewCell.h中已经声明了一个类

    int arrIndex = indexPath.row * waterFlowView.columnCount + indexPath.column;

                // *乘以  +加 列号

    NSDictionary *object = [arrayDataobjectAtIndex:arrIndex];

    NSString *img=[object objectForKey:@"BookIconOtherURL"];

    NSURL *URL = [NSURLURLWithString:img];

    ImageViewCell *imageViewCell = (ImageViewCell *)view;

    imageViewCell.indexPath = indexPath;

    imageViewCell.columnCount = waterFlowView.columnCount;

    [imageViewCell relayoutViews];

    [(ImageViewCell *)view setImageWithURL:URL];

    

 //   UIActivityIndicatorView   

    

}

#pragma mark WaterFlowViewDelegate

- (CGFloat)waterFlowView:(WaterFlowView *)waterFlowView heightForRowAtIndexPath:(IndexPath *)indexPath{


    return 140;    //每行图片的高度

}


- (void)waterFlowView:(WaterFlowView *)waterFlowView didSelectRowAtIndexPath:(IndexPath *)indexPath{


    NSLog(@"indexpath row == %d,column == %d",indexPath.row,indexPath.column);

    //点击图片触发的事件,可以跳转到你项目的其他界面中。

}

- (void)viewDidUnload

{

    [superviewDidUnload];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值