UICollectionView实现分组显示

UICollectionView实现分组显示

2023年7月13日发(作者:)

UICollectionView实现分组显⽰准备⼯作1,新建xib,删除画布上的view对象,并从库中拖⼊Collect View Header View2,打开该对象的attribute inspector设置重⽤id3,新建⼀个类,继承⾃UICollectionReusableView,并将xib中custome类设置为刚才新建的类@interface ElectiveCourse :UIViewController{ BOOL _isLoading; NSMutableArray *_dataArray; NSMutableDictionary *_coursePackDic;}@property(nonatomic,strong)UICollectionView *collectionView;//------ (AppDelegate *)appdelegate;-(void)refreshCoursePackData:(NSInteger)courseId;- (instancetype) initCollectionView;@end//// ElectiveCourse.m// LearningEnglish//// Created by Xu Felicia on 14-5-15.// Copyright (c) 2014年 . All rights reserved.//#import "ElectiveCourse.h"#import "subjectItem.h"#import "studyWordController.h"#import "MJRefresh.h"#import "MeController.h"#import "CollectViewHeaderView.h"@interface ElectiveCourse ()@end@implementation ElectiveCoursestatic NSString *strCellIdentify =@"subjectItemIdentify";- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { =@"选课"; } return self;}static NSString *kCollectionViewHeaderIdentifier =@"cccc";////- (UICollectionViewFlowLayout *) flowLayout{ UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; Direction = UICollectionViewScrollDirectionVertical; if (iPhone5) { ze = CGSizeMake(100, 100); mInteritemSpacing = 20; mLineSpacing = 40; nInset = UIEdgeInsetsMake( 20, 40, 20, 40);

}else { ze = CGSizeMake(100, 100); mInteritemSpacing = 20; mLineSpacing = 20; nInset = UIEdgeInsetsMake( 20, 40, 20, 40); } return layout;}//最初⼀直没实现如下⽅法,导致莫名其妙的错误{ return CGSizeMake(320, 20);}-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)s- (void)addHeader{ __unsafe_unretainedtypeof(self) vc =self; //添加下拉刷新头部控件 [tionView addHeaderWithCallback:^{ // 进⼊刷新状态就会回调这个Block

//模拟延迟加载数据,因此2秒后才调⽤) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [tionView reloadData]; //结束刷新 [tionView headerEndRefreshing]; }); }];}-(void)queryCourseData{ [self refreshCoursePackData:1];}- (void)viewDidLoad{ [super viewDidLoad];

UIView *topNavView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)]; oundColor = [UIColor colorWithRed:33/255.0 green:169/255.0 blue:186/255.0 alpha:1]; [ addSubview:topNavView];

rButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"r"] style:UIBarButtonItemStyleDone target:self action:@

NSString *strPath = [[NSBundle mainBundle] pathForResource:@"Home" ofType:@"plist"]; _coursePackDic = [NSDictionary dictionaryWithContentsOfFile:strPath];

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; Direction = UICollectionViewScrollDirectionVertical;

if (iPhone5) { ze = CGSizeMake(100, 100); mInteritemSpacing = 6; mLineSpacing = 6;

nInset = UIEdgeInsetsMake( 3, 40, 3, 40);

}else { ze = CGSizeMake(100, 100); mInteritemSpacing = 6; mLineSpacing = 6; nInset = UIEdgeInsetsMake( 3, 40, 3, 40); } tionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 320, ScreenHeight -40) collectionViewLayout:layout]; tionViewLayout = layout; BounceVertical =YES; oundColor = [UIColor groupTableViewBackgroundColor]; urce =self; te =self;

[tionView registerClass:[subjectItem class] forCellWithReuseIdentifier:strCellIdentify];

UINib *headerNib = [UINib nibWithNibName:NSStringFromClass([CollectViewHeaderView class]) bundle:[NSBundle mainBundle]];

[ addSubview:tionView];

[self queryCourseData]; [self addHeader];// [self addFooter];

} [tionView registerNib:headerNib forSupplementaryViewOfKind :UICollectionElementKindSectionHeader withReuseIdentifier: kCollectionViewHeaderI//-(void)refreshCoursePackData:(NSInteger)courseId{ NSString *skey = [NSStringstringWithFormat:@"p%d",courseId]; NSMutableDictionary *dic =_coursePackDic[skey]; _dataArray = [dicobjectForKey:@"data"]; = dic[@"title"]; [tionViewreloadData];

}-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return_;}-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ NSArray *arr =_dataArray[section][@"courses"]; return ;}-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath{

= [NSString stringWithFormat:@"——%@——",_dataArray[n][@"title"]]; return view;}-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ subjectItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strCellIdentify forIndexPath:indexPath]; /* NSDictionary *dic = _dataArray[]; [cell initWithDic:dic];

int idx = [[dic objectForKey:@"imgidx"] intValue]; int64_t nId = 100002 + idx * 3; */

int64_t nId = 100002 + (arc4random() % 100 * 3);

NSString *strURL = [NSString stringWithFormat:@"/images/%",nId]; NSURL *url = [NSURL URLWithString:strURL]; [iew setImageWithURL:url placeholderImage:[UIImage imageNamed:@"image_home_replace"]];

= ( == 0?nil:[UIImage imageNamed:@"image_home_selflag"]);;

return cell;}@end CollectViewHeaderView *view = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"cccc运⾏效果如下

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689263866a226394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信