1. 程式人生 > >iOS 瀑布流的簡單用法

iOS 瀑布流的簡單用法

- (void)viewDidLoad
{
    [super viewDidLoad];


    mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:mainScrollView];


    pullArray  = [NSMutableArray array];

    [HttpRequset requsetForCurrentPage:1 completon:^(NSDictionary *responseDic) {
        NSLog
(@"responseDic===%@",responseDic); NSDictionary * data = responseDic[@"data"]; NSArray * items = data[@"items"]; for (int i = 0; i < items.count; i ++) { PullModel * model = [[PullModel alloc] initWithJsonDic:items[i]]; [pullArray addObject:model]; } [self
loadPullView]; }]; } -(void)loadPullView { //一維陣列 分別代表左右兩列 float colHeight[2] = {0.0,0.0}; for (PullModel * model in pullArray) { float height = [model.height floatValue]; float width = [model.width floatValue]; // NSLog(@"height=====%f,width====%f",height,width);
//重新計算圖片寬高 height =height * 150/width; width = 150; int index = colHeight[0] <= colHeight[1] ? 0 : 1; float x = index == 0 ? 0:160; float y = colHeight[index]; UIImageView * showImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)]; [showImageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl]]; NSLog(@"model.url==%@",model.picUrl); [mainScrollView addSubview:showImageView]; colHeight[index] += height + 20; } float height = colHeight[0] <= colHeight[1] ? colHeight[1]:colHeight[0]; [mainScrollView setContentSize:CGSizeMake(320, height)]; }