IOS waterfall is implemented through the UICollectionView Control

Source: Internet
Author: User

IOS waterfall is implemented through the UICollectionView Control

Some IOS projects use the waterfall stream function. You can choose to use a third-party library or write one by yourself. If you write this function yourself, you can use the UICollectionView Control for display, the settings are divided into several columns.

The Code is as follows:

 

# Define screenHeight [[UIScreen mainScreen] bounds]. size. height // screen height # define screenWidth [[UIScreen mainScreen] bounds]. size. width // screen width # define colletionCell 3 // set the specific columns @ interface HomeViewController () {UICollectionView * collectionView; NSMutableArray * hArr; // record the height of each cell} @ end @ implementation HomeViewController-(void) viewDidLoad {[super viewDidLoad]; hArr = [[NSMutableArray alloc] init]; optional * flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setScrollDirection: direction]; // you can specify horizontal or vertical collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake (0, 0, screenWidth, screenHeight) collectionViewLayout: flowLayout]; collectionView. dataSource = self; collectionView. delegate = self; [collectionView setBackgroundColor: [UIColor clearColor]; [collectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: @ UICollectionViewCell]; [self. view addSubview: collectionView] ;}# pragma mark -- UICollectionViewDataSource // defines the number of uicollectionviewcells displayed-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return 20 ;} // define the number of displayed sections-(NSInteger) numberOfSectionsInCollectionView :( UICollectionView *) collectionView {return 1;} // The content displayed for each UICollectionView-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ UICollectionViewCell; UICollectionViewCell * cell = [collectionView progress: CellIdentifier forIndexPath: indexPath]; cell. backgroundColor = [UIColor colorWithRed :( (10 * indexPath. row)/255.0) green :( (20 * indexPath. row)/255.0) blue :( (30 * indexPath. (row)/255.0) alpha: 1.0f]; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, 0, 20, 20)]; label. textColor = [UIColor redColor]; label. text = [NSString stringWithFormat: @ % d, indexPath. row]; // remove cell for (id subView in cell. contentView. subviews) {[subView removeFromSuperview];} NSInteger remainder = indexPath. row % colletionCell; NSInteger currentRow = indexPath. row/colletionCell; CGFloat currentHeight = [hArr [indexPath. row] floatValue]; CGFloat positonX = (screenWidth/colletionCell-8) * remainder + 5 * (remainder + 1); CGFloat positionY = (currentRow + 1) * 5; for (NSInteger I = 0; I
 
  
Implementation result:
  

 

 

Related Article

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.