photoWall自定义

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:_scrollView];
        
        _cellArray = [NSMutableArray array];
        
        _numberOfColumns = 4;
        _spacing=5;
        _edgeInset=UIEdgeInsetsMake(20, 0, 20, 0);
    }
    
    return self;
}

- (void)reloadData
{
    //重新加载数据前需要将老数据清理掉

    //获取视图(小格子)的个数
    NSInteger count = [_dataSource numberOfViewInPhotoWall:self];
    
    CGFloat width = self.bounds.size.width / _numberOfColumns;
    CGFloat height = 44.0f;
    if (_dataSource && [_dataSource respondsToSelector:@selector(photoWall:cellHeightAtIndex:)]) {
        
    }
    
    //计算滚动视图的大小(有问题,自己算..... 高度不对)
    int heiCount;
    if(count%_numberOfColumns)
    {
        heiCount=count/_numberOfColumns+1;
    }
    else
    {
        heiCount=count/_numberOfColumns;
    }
    _scrollView.contentInset=_edgeInset;
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width,heiCount * height);
    
    //总数与index相差1
    for (NSInteger i = 0; i < count; i++) {
        MyPhotoWallCell *cell = [_dataSource photoWall:self cellAtIndex:i];
        [cell addTarget:self action:@selector(didClicked:) forControlEvents:UIControlEventTouchUpInside];
        cell.tag = i;
        if (_dataSource&&[_dataSource respondsToSelector:@selector(photoWall:cellHeightAtIndex:)]) {
            height = [_dataSource photoWall:self cellHeightAtIndex:i];
        }
        CGFloat y = (i / _numberOfColumns) * height;
        NSLog(@"%f--%f", width * i, y);
        cell.frame = CGRectMake(width * (i % _numberOfColumns), y, width-_spacing, height-_spacing);
        [_scrollView addSubview:cell];
    }
}

- (void)didClicked:(MyPhotoWallCell *)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(photoWall:didSelectedIndex:)]) {
        [_delegate photoWall:self didSelectedIndex:sender.tag];
    }
}



//
//  ViewController.m
//  MyCustomPhotoWall
//
//  Created by apple on 14-8-22.
//  Copyright (c) 2014年 戴维营教育. All rights reserved.
//

#import "ViewController.h"

#import "MyPhotoWallCell.h"
#import "MyPhotoWall.h"

@interface ViewController () <MyPhotoWallDataSource, MyPhotoWallDelegate>

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    
    MyPhotoWall *photoWall = [[MyPhotoWall alloc] initWithFrame:CGRectMake(10, 10, 300, 460)];
    photoWall.backgroundColor = [UIColor redColor];
    photoWall.dataSource = self;
    photoWall.delegate = self;
    [self.view addSubview:photoWall];
    
    [photoWall reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfViewInPhotoWall:(MyPhotoWall *)photoWall
{
    return 100;
}

- (CGFloat)photoWall:(MyPhotoWall *)photoWall cellHeightAtIndex:(NSInteger)index
{
    if (index%2) {
        return 94;
    }
    else
    {
        return 55;
    }    
}

- (MyPhotoWallCell *)photoWall:(MyPhotoWall *)photoWall cellAtIndex:(NSInteger)index
{
    MyPhotoWallCell *cell = [[MyPhotoWallCell alloc] init];
    
    if (index % 3) {
        cell.backgroundColor = [UIColor purpleColor];
    }
    else if (index % 2) {
        cell.backgroundColor = [UIColor magentaColor];
    }
    else {
        cell.backgroundColor = [UIColor cyanColor];
    }
    
//    if (index % 2) {
//        cell.backgroundColor = [UIColor purpleColor];
//    }
//    else {
//        cell.backgroundColor = [UIColor cyanColor];
//    }

    return cell;
}

- (void)photoWall:(MyPhotoWall *)photoWall didSelectedIndex:(NSInteger)index
{
    NSLog(@"Selected: %d", index);
}
@end
原文地址:https://www.cnblogs.com/lidongq/p/3929907.html