1.先放張效果圖
2.如果想在TableView 上放 CollectionView, 最好要寫在TableViewCell里, 並且CollectionView的代理方法都要寫在TableViewCell里, 如果在ViewController里寫的話,會因為cell重用的問題出現崩潰, 並且注冊CollectionView的cell的時候, 會出現很多問題.
下邊是代碼:在TableView的Cell里, 寫CollectionView的代理方法, 注冊CollectionView的cell, 希望對朋友們有幫#import "SecondTableViewCell.h"#import "SecondCollectionViewCell.h#import "UrlMacro.h"#import "FiveCollectionViewCell.h"
#import "SecondViewController.h" #import "UIImageView+WebCache.h" #import "Picture.h" #import "CompleteViewController.h" #import "ContentViewController.h" #import "FiveCollectionViewCell.h" @implementation SecondTableViewCell - (void)dealloc { [_moreButton release]; [_topLabel release]; [super dealloc]; } //重寫Array的方法 -(void)setArray:(NSMutableArray *)array { if (_array != array) { [_array release]; _array = [array retain]; } //NSLog(@"00000000000%@", array);
//刷新數據
[self.collectionView reloadData]; }
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.topLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, VPOINT(10), HPOINT(120), VPOINT(20))]; //self.topLabel.backgroundColor = [UIColor colorWithRed:1.000 green:0.265 blue:0.882 alpha:1.000]; self.topLabel.textAlignment = NSTextAlignmentLeft; self.topLabel.font = [UIFont systemFontOfSize:15]; [self.contentView addSubview:_topLabel]; [_topLabel release]; self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.moreButton.frame = CGRectMake(KWIDTH - KWIDTH / 4, VPOINT(10), HPOINT(110), VPOINT(20)); self.moreButton.titleLabel.font = [UIFont systemFontOfSize:15]; [self.moreButton setTitle:@"查看更多" forState:UIControlStateNormal]; [self.moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; self.moreButton.titleLabel.textAlignment = NSTextAlignmentRight; //self.moreButton.backgroundColor = [UIColor redColor]; //[self.moreButton addTarget:self action:@selector(clickMoreButton:) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:_moreButton];
//在TableViewcell里調用getCollection方法 [self getCollection]; } return self; } -(void)getCollection {
//CollectionView UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setItemSize:CGSizeMake(HPOINT(120), VPOINT(140))]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5); self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, VPOINT(26), KWIDTH, VPOINT(160)) collectionViewLayout:flowLayout] autorelease]; self.collectionView.backgroundColor = [UIColor whiteColor]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.collectionView registerClass:[SecondCollectionViewCell class] forCellWithReuseIdentifier:@"123"]; [self.collectionView registerClass:[FiveCollectionViewCell class] forCellWithReuseIdentifier:@"456"]; [self.contentView addSubview:_collectionView]; [_collectionView release]; } - (void)awakeFromNib { // Initialization code }
//CollectionView的分區數 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } //item個數 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.array.count; } // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//這里是因為第五組數據跟其他數據不一樣, 所以在這做了判斷 NSLog(@"xxxxxxxxxxxxxxxxx%ld", self.count); if (self.count != 4) { SecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"123" forIndexPath:indexPath]; //cell.backgroundColor = [UIColor yellowColor]; Picture *picture = self.array[indexPath.item]; //NSLog(@"....................%@", picture); [cell.imageView sd_setImageWithURL:[NSURL URLWithString:picture.cover_image_url]]; cell.titleLabel.text = picture.nameTitle; cell.nameLabel.text = picture.nickname; return cell; } else {
//這是給第五條數據自定義的CollectionViewCell FiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"456" forIndexPath:indexPath]; //cell.backgroundColor = [UIColor yellowColor]; Picture *picture = self.array[indexPath.item]; //NSLog(@"....................%@", picture); [cell.imageView sd_setImageWithURL:[NSURL URLWithString:picture.cover_image_url]]; cell.titleLabel.text = picture.nameTitle; cell.nameLabel.text = picture.nickname; return cell; } } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.count != 4) {
//這里第五組數據跟其他數據跳轉的下一個頁面也不一樣, 判斷一下 CompleteViewController *completeVC = [CompleteViewController new]; Picture *picture = self.array[indexPath.item]; NSLog(@"..............%ld", picture.ID); completeVC.urlNetwork = [NSString stringWithFormat:@"http://api.kuaikanmanhua.com/v1/topics/%ld?sort=0", picture.ID]; [self.second.navigationController pushViewController:completeVC animated:YES]; [completeVC release]; [picture release]; } else { ContentViewController *contentView = [ContentViewController new]; Picture *picture = self.array[indexPath.item]; NSLog(@"%@", picture.comic_id); contentView.url = [NSString stringWithFormat:@"http://api.kuaikanmanhua.com/v1/comics/%@", picture.comic_id]; contentView.commentUrl = [NSString stringWithFormat:@"http://api.kuaikanmanhua.com/v1/comics/%@/hot_comments", picture.comic_id]; [_second.navigationController pushViewController:contentView animated:YES]; [picture release]; } } // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: //定義每個UICollectionViewCell 的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(120, 140); } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 0; } -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(5, 5, 5, 5);//分別為上、左、下、右 } //每個section中不同的行之間的行間距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10; } //取消選擇了某個cell - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { // UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; // [cell setBackgroundColor:[UIColor redColor]]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end