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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值