技术文章分类(180)

技术随笔(11)

ios自定义UICollectionViewCell注意事项

如果你熟练UITableview的用法,那么恭喜你,很简单了。UICollectionView只比UItableview需要多设置一个属性,那就是colloctionView.collectionViewFlowLayout.

   UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(61, 61);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumInteritemSpacing = 10.0f;
    layout.minimumLineSpacing = 10.0f;
    self.collectionview.collectionViewLayout = layout;

其他基本一样,或者说完全一样。但是设置layout其实还有一种方式,是同过实现collectionViewFlowLayoutDelegate方法来达到效果的,我个人认为上面显得更简单一些。

 

以下也是一种详细方法,跟上面略有不同。

1,新建Cell.xib,Cell.h,Cell.m文件,关联文件和属性,并设置identifier(这里设置为Cell)

2,  郑重承诺:以下没有多余的代码。

原理跟UItableview是一样的。

首先以xib和identifier把Cell注册到collectionView;

之后通过identifier和indexpath在collctionView中取到你要的cell

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

.m

#import "ViewController.h"
#import "Cell.h"

@interface ViewController ()
{
    UICollectionView *collectionview;
    NSString *identifier;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    identifier = @"Cell";
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 640, 100) collectionViewLayout: layout];
    
    UINib *nib = [UINib nibWithNibName:@"Cell" bundle:nil];
    [collectionview registerNib:nib forCellWithReuseIdentifier:identifier];
    
    collectionview.delegate = self;
    collectionview.dataSource = self;
    collectionview.backgroundColor = [UIColor redColor];
    [self.view addSubview:collectionview];
}

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

#pragma UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 100;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.titleLabel.text = @"title";
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
    
}

#pragma UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(97, 31);
}

#pragma UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    NSLog(@"didSelect:%d",row);
}
@end

 

建议在viewDidLoad方法中加上下面代码:

self.automaticallyAdjustsScrollViewInsets = NO;//解决cellForItemAtIndexPath not called问题

posted @ 2014-08-10 16:34  坤哥MartinLi  阅读(2110)  评论(0编辑  收藏  举报