Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use UICollectionView in ios

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use UICollectionView in ios, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

-(void) viewDidLoad {

[super viewDidLoad]

Self.title = @ "basic use of UICollectionView"

Self.view.backgroundColor = [UIColor whiteColor]

UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init]

UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]

CollectionView.delegate = self

CollectionView.dataSource = self

[collectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier:@ "cellID"]

[self.view addSubview:collectionView]

/ / Do any additional setup after loading the view.

}

# Total number of pragma mark UICollectionView-delegate and dataSource-UICollectionViewCell

-(NSInteger) collectionView: (UICollectionView *) collectionView numberOfItemsInSection: (NSInteger) section {

Return 100

}

# pragma mark UICollectionView-delegate and dataSource every UICollectionViewCell

-(UICollectionViewCell *) collectionView: (UICollectionView *) collectionView cellForItemAtIndexPath: (NSIndexPath *) indexPath {

Static NSString * cellID = @ "cellID"

UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]

If (cell = = nil) {

Cell = [[UICollectionViewCell alloc] init]

}

Cell.backgroundColor = [UIColor grayColor]

Return cell

}

# pragma mark-- UICollectionViewDelegate sets the size of each UICollectionView

-(CGSize) collectionView: (UICollectionView *) collectionView layout: (UICollectionViewLayout*) collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *) indexPath

{

Return CGSizeMake (CGRectGetWidth (self.view.bounds) / 3-10, CGRectGetWidth (self.view.bounds) / 3-10)

}

# number of pragma mark-CollectionView section

-(NSInteger) numberOfSectionsInCollectionView: (UICollectionView *) collectionView

{

Return 1

}

# pragma mark- sets the spacing of each UICollectionView

-(UIEdgeInsets) collectionView: (UICollectionView *) collectionView layout: (UICollectionViewLayout *) collectionViewLayout insetForSectionAtIndex: (NSInteger) section {

Return UIEdgeInsetsMake (0Jol 0,0Jol 0)

}

# pragma mark- sets the vertical spacing of each UICollectionView

-(CGFloat) collectionView: (UICollectionView *) collectionView layout: (UICollectionViewLayout *) collectionViewLayout minimumInteritemSpacingForSectionAtIndex: (NSInteger) section {

Return 10

}

# pragma mark- sets the horizontal spacing of each UICollectionView

-(CGFloat) collectionView: (UICollectionView *) collectionView layout: (UICollectionViewLayout*) collectionViewLayout minimumLineSpacingForSectionAtIndex: (NSInteger) section {

Return 10

}

# Click events of pragma mark-UICollectionView

-(void) collectionView: (UICollectionView *) collectionView didSelectItemAtIndexPath: (NSIndexPath *) indexPath {

NSLog (@ "% @", @ (indexPath.row) .description)

}

The above is all the contents of the article "how to use UICollectionView in ios". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report