UITableView UICollectionView的操作

UITableView使用

一、代码自定义cell

  集成uitableviewcell,并重写initwithStyle方法,在InitwithStyle里面add视图,调用[tableView dequeueReusableCellWithIdentifier:cellID]时会自动调用cell的initWithStyle方法。

二、xib自定义cell

  创建cell xib,添加它的Custom Class。xib里面的这个属性 inherite module from target
image.png

网上搜了下这个属性,也没太说明白啥意思,反正在勾与不勾之间。

三、使用

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //调用频繁,声明为静态变量
    static NSString *cellIdentifier=@"identify";
    //缓存里获取
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];//
    }
    cell.data = data;
    return cell;
}

如果是table已经注册了该cell,直接调用dequeueReusable就可以获取响应的cell,不需要直接调用initwithstyle。

[self.table registerNib:[UINib nibWithNibName:@"nibName" bundle:nil] forCellReuseIdentifier:@"identify"];
cellForRow里面就直接调用,无需直接调用initwithstyle
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

dequeueReusableCellWithIdentifier:forIndexPath

这个方法需要和register方法一起用

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;  
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier;

这样也会自动调用initWithStyle:withReuseableCellIdentifier方法创建新的cell

UICollectionView使用

#pragma mark 初始化
- (void) initCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置CollectionView的属性
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 220, DeviceSize.width, DeviceSize.height - 220) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = UIColor.white;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = YES;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"nibName" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"cell"];
}
#pragma mark  设置CollectionView的组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark  设置CollectionView每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.data.count;
}

#pragma mark  设置CollectionCell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strCell = @"cell";
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strCell forIndexPath:indexPath];
    Data *p = self.data[indexPath.row];
    cell.data = p;
    return cell;
}

#pragma mark  定义每个UICollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(100,100);
}

#pragma mark  定义整个CollectionViewCell与整个View的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0,10, 0);
}

#pragma mark  每个UICollectionView的水平方向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

#pragma mark  每个UICollectionView竖直方向
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

#pragma mark  点击CollectionView
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cell *p = self.data[indexPath.item];
}

#pragma mark  cell是否可点击
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

添加header footer

注:uicollection item元素,header、footer元素的大小、布局都是走的UICollectionViewDelegateFlowLayout的代理,不是走的UICollection的代理

//创建UICollectionReusableView的子类并且Collectionview注册
[self.collectionView registerNib:[UINib nibWithNibName:@"CollHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifierHeader];
[self.collectionView registerNib:[UINib nibWithNibName:@"CollFooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseIdentifierFooter];

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
 UICollectionReusableView *supplementaryView;
 
 if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
 CollHeaderView *view = (CollHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifierHeader forIndexPath:indexPath];
     supplementaryView = view;
 }
 else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
 CollFooterView *view = (CollFooterView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseIdentifierFooter forIndexPath:indexPath];
     supplementaryView = view;
 }
     return supplementaryView;
}
//实现UICollectionViewDelegateFlowLayout的代理 设置header footer的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

你可能感兴趣的:(UITableView UICollectionView的操作)