关于TableView 上放 CollectionView


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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM