UICollectionView 简单实现

来源:互联网 发布:网络兼职广告语大全集 编辑:程序博客网 时间:2024/05/24 05:28

- (void)viewDidLoad{    [super viewDidLoad];        ////    1. 添加头部控件的方法//    [_collection addHeaderWithTarget:self action:@selector(headerRereshing)];////    或者////    [self.tableView addHeaderWithCallback:^{ }];//    ////    2. 添加尾部控件的方法//    [_collection addFooterWithTarget:self action:@selector(footerRereshing)];    //    [self.tableView addFooterWithCallback:^{ }];    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ham.png"]  style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Refresh.png"] style:UIBarButtonItemStylePlain target:self action:@selector(RefreshAction:)];        // Do any additional setup after loading the view.    self.navigationItem.title= @"首页";            self.navigationController.navigationBar.translucent=NO;    UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout alloc]init];    layout.itemSize = CGSizeMake(130, 120);//方块大小    layout.minimumLineSpacing = 10;//纵间距    layout.minimumInteritemSpacing = 10 ;//行间距    layout.scrollDirection =UICollectionViewScrollDirectionVertical;//纵向    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);//方块与边框上左下右间距       //创建collectionview    _collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-64-49) collectionViewLayout:layout];    [layout release];    [_collection setBackgroundColor:[WebColor antiqueWhite]];    _collection.delegate=self;    _collection.dataSource=self;    [_collection registerClass:[HomepageViewUICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];    [self.view addSubview:_collection];}#pragma mark-#pragma mark- collectionview必须实现的俩个协议- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return [self.collectionArray count];}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    HomepageViewUICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];    HomePageModel  * model=[self.collectionArray objectAtIndex:indexPath.row];    cell.title.text= model.title;    cell.number.text=model.number;    [cell.images setImageWithURL:[NSURL URLWithString:model.titleimage]];//    NSLog(@"%d",indexPath.row);        return cell;}#pragma mark-#pragma mark- collectionview点击事件- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{       TwoHomepageViewController * two=[[TwoHomepageViewController alloc]init];    HomePageModel * model=[_collectionArray objectAtIndex:indexPath.row];    two.takeId=model.idNum;     [self.navigationController pushViewController:two animated:YES];    two.hidesBottomBarWhenPushed = YES;//隐藏tabBar    [two release];}


0 0
原创粉丝点击