一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

iOS不自定义UICollectionViewCell直接在代理方法实现

时间:2022-06-25 23:40:12 编辑:袖梨 来源:一聚教程网

众所周知,collectionView在数据源方法中,需要加入自定义的cell,有时候一个简单的cell,只需要很简单的UI,这时候自定义,就显得多此一举,在此,推荐一种自定义方法。可以解决重用问题,还能快速创建。

UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议

 代码如下 复制代码

 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (style ==1) {
        KeepCarBrandCollectionViewCell *cell = (KeepCarBrandCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"KeepCarBrandCollectionViewCell" forIndexPath:indexPath];
        [cell setBackgroundColor:kYellowColor];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kImageBaseURL,_imageArray[indexPath.row]]];
 
        [cell.iconImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"pic_common_carlogo_loading"]];
        NSLog(@"%@",url);
        [cell.iconImageView setContentMode:UIViewContentModeScaleAspectFit];
        [cell.brandNameLabel setText:_titleArray[indexPath.row]];
        return cell;
    }else if (style == 2)
    {
        UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CarTypeCell" forIndexPath:indexPath];
        if (cell) {
            [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        }
        //边框view
        UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, cell.width-10, cell.height-10)];
        [borderView setBorder:1 color:kLineColor];
        [cell addSubview:borderView];
        //内容label
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:borderView.frame];
        contentLabel.font = FONT(16);
        if (_headerArray[indexPath.section]) {
             contentLabel.text = [[_contentDic objectForKey:[_headerArray[indexPath.section] objectForKey:@"id"]][indexPath.row] objectForKey:@"name"];
        }
        contentLabel.textAlignment = NSTextAlignmentCenter;
        [cell addSubview:contentLabel];
        return cell;
    }else if (style ==3)
    {
       UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"OutPutCell" forIndexPath:indexPath];
        if (cell) {
            [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        }
        //边框view
        UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, cell.width-10, cell.height-10)];
        [borderView setBorder:1 color:kLineColor];
        [cell addSubview:borderView];
        //内容label
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:borderView.frame];
        contentLabel.font = FONT(16);
        contentLabel.text = _contentArray[indexPath.row];
        contentLabel.textAlignment = NSTextAlignmentCenter;
        [cell addSubview:contentLabel];
        return cell;
    }
    return nil;
}

红色高亮区域,便是代理方法里面的具体实现,其实就是在加载cell的时候,将他的所有子空间全部移除,以免其复用的时候,出现重复的子空间。

热门栏目