你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS7技巧 >> iOS不自定義UICollectionViewCell直接在代理方法實現

iOS不自定義UICollectionViewCell直接在代理方法實現

編輯:IOS7技巧
collectionviewCell小編使用到的不多不過今天看到有一個朋友分享了下面小編一為各位介紹吧,下文我們來看一篇iOS不自定義collectionviewCell直接在代理方法實現例子。

眾所周知,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的時候,將他的所有子空間全部移除,以免其復用的時候,出現重復的子空間。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved