瀑布流教程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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章