接教程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