In general, there are two main ways to achieve waterfall flow. Method One: Use UITableView. Method Two: Use Uiscrollview.
First Introduce method One (also the official recommended way)
1. Always make a few columns first to be clear beforehand, how many records.
2. Suppose to make 3 columns, use three uitableview, width average, high dynamic, page height take uitableview highest.
3. Three uitableview initialized with tag (I'm getting the feeling that tag is very useful in iOS, just like reading the ID in the HTML control in JS), and Showsverticalscrollindicator and scrollenabled are set to No,separatorstyle to Uitableviewcellseparatorstylenone and added to UIView.
Get height
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section?
{ ?
Return when row record number/column;
?}
?-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{?
int arrindex= Current Indexpath.row * Column (3) + current indexpath.column;|?
return [[Xml/json Objectatindex:arrindex] objectforkey:@ "height"];?} ?
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{?
The data from the current array is obtained from the data source and then populated with UITableViewCell
?}
Method one is relatively simple to implement. But there are some drawbacks: 1, to operate three uitableview, locating the cell in the current point is more troublesome. The pictures in 2.cell are all equal height. In reality the picture size is not the same, there is high there is low. This is also a lot of the limitations of the method. So recommendation method Two.
Method Two:
Principle: Place the picture on the Uiscrollview according to the picture size. Here are two questions: 1. How to calculate the starting position of each picture. 2. How do I save memory from performance considerations? This requires a cell reuse mechanism.
If you write yourself from scratch, there are a few steps below:
1. The base class is a uiscrollview. are operated on this basis.
2. Some are directly using the cell (the cell in this cell is not a uitablevie, this is similar to a uiview), and some add a layer of UITableView (this step is actually a bit redundant).
3. Get information about pictures (title,width,height, URLs, etc.) from the network or from the configuration file.
4. Based on the information of the picture, calculate the starting position of the UIView and save the memory (note the boundary).
5.reloadData, start drawing the cell (with a network request, use the Sdwebimag library to get the picture).
6. If the trigger gesture (swipe, drag up or down) and draw again if there is a reusable cell (these are stored in memory).
Oh, the above 6 steps is probably. There is no practical effect (I think). Just when I say it.
Take advantage of an open source library below. Provide a valid demo (or you can't make it in your heart)
Original address: Https://github.com/1000Memories/TMQuiltView
I modified it on the basis of his.
Establish a Testquiltview project.
Copy the following files and join the project.
TMPhotoQuiltViewCell.h
tmphotoquiltviewcell.m
TMQuiltView.h
tmquiltview.m
TMQuiltViewCell.h
tmquiltviewcell.m
Call file
MyTMQuiltViewController.h
#import <UIKit/UIKit.h>
#import "TMQuiltView.h"
#import "TMPhotoQuiltViewCell.h"
@interface Mytmquiltviewcontroller:uiviewcontroller <TMQuiltViewDataSource,TMQuiltViewDelegate> {
}
@property (strong,nonatomic) Tmquiltview *quiltview;
@property (strong,nonatomic) Nsarray *images;
@end
Mytmquiltviewcontroller.m
#import "MyTMQuiltViewController.h"
@interface Mytmquiltviewcontroller ()
@end
#define Knumberofcells 1000
@implementation Mytmquiltviewcontroller
-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil
{
self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];
if (self) {
Custom initialization
}
return self;
}
-(void) Dealloc {
[_quiltview release];
_quiltview = nil;
[_images release];
_images = nil;
[Super Dealloc];
}
-(void) viewdidload
{
[Super Viewdidload];
_quiltview = [[Tmquiltview alloc] initWithFrame:self.view.bounds];
_quiltview.datasource = self;
_quiltview.delegate = self;
[Self.view Addsubview:_quiltview];
[_quiltview Reloaddata];
}
-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
#pragma mark-quiltviewcontrollerdatasource
-(Nsarray *) Images {
if (!_images) {
Nsmutablearray *imagenames = [Nsmutablearray array];
for (int i = 0; i < knumberofcells; i++) {
[Imagenames addobject:[nsstring stringwithformat:@ "%d.jpeg", I% 10 + 1]];
}
_images = [imagenames retain];
}
return _images;
}
-(UIImage *) Imageatindexpath: (Nsindexpath *) Indexpath {
return [UIImage imagenamed:[self.images ObjectAtIndex:indexPath.row]];
}
-(Nsinteger) Quiltviewnumberofcells: (Tmquiltview *) Tmquiltview {
return [self.images Count];
}
-(Tmquiltviewcell *) Quiltview: (Tmquiltview *) Quiltview Cellatindexpath: (Nsindexpath *) Indexpath {
Tmphotoquiltviewcell *cell = (Tmphotoquiltviewcell *) [Quiltview dequeuereusablecellwithreuseidentifier:@ "PhotoCell" ];
if (!cell) {
cell = [[[Tmphotoquiltviewcell alloc] initwithreuseidentifier:@ "Photocell"] autorelease];
}
Cell.photoView.image = [self imageatindexpath:indexpath];
Cell.titleLabel.text = [NSString stringwithformat:@ "%d", Indexpath.row + 1];
return cell;
}
#pragma mark-tmquiltviewdelegate
-(Nsinteger) Quiltviewnumberofcolumns: (Tmquiltview *) Quiltview {
return 2;
}
-(CGFloat) Quiltview: (Tmquiltview *) Quiltview Heightforcellatindexpath: (Nsindexpath *) Indexpath {
return [self imageatindexpath:indexpath].size.height/[self quiltviewnumberofcolumns:quiltview];
}
@end
In main
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
_mytmquitviewcontroller = [[Mytmquiltviewcontroller alloc] Initwithnibname:nil Bundle:nil];
[_mytmquitviewcontroller.view Setbackgroundcolor:[uicolor Clearcolor];
[_window.rootviewcontroller =_mytmquitviewcontroller;
}
iOS Waterfall stream