UICollectionView basics, uicollectionview

Source: Internet
Author: User

UICollectionView basics, uicollectionview

UICollectionView is often used in iOS development. UIScrollView is the same as UITableView, but operations are more complicated than UITableVIew. In some cases, you need to pay attention to the following details. //// ViewController. m // Collection # import "ViewController. h "# import" CollectionViewCell. h "# import" CollectionViewCell1.h "# import" CollectionReusableView. h "@ interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource> @ property (nonatomic, strong) UICollectionView * collectionView; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // The UICollectionViewFlowLayout object must be created here. * layout = [[UICollectionViewFlowLayout alloc] init]; // The minimum interval between two adjacent cells in the same row is layout. minimumInteritemSpacing = 5; // The interval between the minimum two rows is layout. minimumLineSpacing = 5; _ collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake (0, 0,375,667) collectionViewLayout: layout]; _ collectionView. backgroundColor = [UIColor whiteColor]; _ collectionView. delegate = self; _ collectionView. dataSource = self; // This is a horizontal slide // layout. scrollDirection = uicollectionviewscrolldirehorhorizontal; [self. view addSubview: _ collectionView];/** this is the focus of cell registration. * // This type of cell is created by xib. You need to register UINib * cellNib = [UINib nibWithNibName: @ "CollectionViewCell" bundle: nil]; [_ collectionView registerNib: cellNib forCellWithReuseIdentifier: @ "CollectionViewCell"]; // This is the registration of a custom cell without xib // [_ collectionView registerClass: [CollectionViewCell1 class] forCellWithReuseIdentifier: @ "myheidentifier"]; // register the native cell. // [_ collectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: @ "cell"]; // This is the registration UINib * cellNib1 = [UINib nibWithNibName: @ "CollectionReusableView" bundle: nil]; [_ collectionView registerNib: cellNib1 environment: Environment: @ "CollectionReusableView"]; [_ collectionView registerNib: cellNib1 metadata: Invalid withReuseIdentifier: @ "CollectionReusableView"];} // The total number of groups-(NSInteger) Metadata :( UICollectionView *) collectionView {return 1;} // number of cells in each group-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return 10 ;} // What is each cell-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {CollectionViewCell * cell = [collectionView detail: @ "CollectionViewCell" forIndexPath: indexPath]; cell. label. text = [NSString stringWithFormat: @ "% ld", indexPath. section * 100 + indexPath. row]; cell. backgroundColor = [UIColor gradient]; return cell;} // head and foot loading-(UICollectionReusableView *) collectionView :( UICollectionView *) collectionView detail :( NSString *) kind atIndexPath :( NSIndexPath *) indexPath {UICollectionReusableView * view = [collectionView attributes: kind attributes: @ "CollectionReusableView" forIndexPath: indexPath]; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (110, 20,100, 30)]; if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {label. text = @ "Header";} else {label. text = @ "";} [view addSubview: label]; return view;} // the upper left and lower right margin of each Group-(UIEdgeInsets) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout insetForSectionAtIndex :( NSInteger) section {return UIEdgeInsetsMake (5, 5, 5, 5);} // the size of the header attempt-(CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout referenceSizeForHeaderInSection :( NSInteger) section {return CGSizeMake (50, 60);} // the size of the foot (CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout referenceSizeForFooterInSection :( NSInteger) section {return CGSizeMake (50, 60);} // defines the size of each cell (CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath :( NSIndexPath *) indexPath {return CGSizeMake (115,100);} // cell Click Event-(void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath {// The animation after the cell is clicked [collectionView selectItemAtIndexPath: indexPath animated: YES scrollPosition: Finished];} @ end

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.