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

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.