UI-瀑布流的做法!
//在MainViewController.h文件中定义三个放在self.view上的三个Label变量;
@interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_leftTable;
UITableView *_centerTable;
UITableView *_rightTable;
NSMutableArray *_imageArray;
}
@end
//在MainViewController.m文件中初始化_imageArray(添加图片数量的数组);
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_imageArray = [[NSMutableArray alloc] init];
for (int i = 1; i < 20; i++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];//自动释放池
NSString *name = [NSString stringWithFormat:@"%d.jpg",i];
[_imageArray addObject:name];
[pool drain];
}
}
return self;
}
//定义Label,根据_imageArray数量决定的line和row,设定Label的大小位置;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_leftTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320.0/3, 500) style:UITableViewStylePlain];
[_leftTable setDelegate:self];
[_leftTable setDataSource:self];
[self.view addSubview:_leftTable];
[_leftTable release];
_centerTable = [[UITableView alloc] initWithFrame:CGRectMake(320/3, 20, 320/3, 500) style:UITableViewStylePlain];
[_centerTable setDelegate:self];
[_centerTable setDataSource:self];
[self.view addSubview:_centerTable];
[_centerTable release];
_rightTable = [[UITableView alloc] initWithFrame:CGRectMake(320/3*2, 20, 320/3, 500) style:UITableViewStylePlain];
[_rightTable setDelegate:self];
[_rightTable setDataSource:self];
[self.view addSubview:_rightTable];
[_rightTable release];
}
#pragma mark -
#pragma mark TableView Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger index = indexPath.row * 3;
if (_centerTable == tableView) {
index++;
}
if (_rightTable == tableView) {
index += 2;
}
NSString *name = [_imageArray objectAtIndex:index];
UIImage *aImage = [UIImage imageNamed:name];
CGSize size = aImage.size;
CGFloat scale = size.width/(320/3);
CGFloat height = size.height/scale;
return height;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger count = [_imageArray count]/3;
NSInteger value = [_imageArray count]%3;
if (_leftTable == tableView) {
if (value>0 ) {
count++;
}
}
if (_centerTable == tableView) {
if (value>1) {
count ++;
}
}
return count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentify = @"cellIdentify";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (!cell) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
}
NSInteger index = indexPath.row * 3;
if (_centerTable == tableView) {
index++;
}
if (_rightTable == tableView) {
index += 2;
}
NSString *name = [_imageArray objectAtIndex:index];
NSLog(@"图片之前");
[cell.waterFallImage setImage:[UIImage imageNamed:name]];
NSLog(@"图片之后");
return cell;
}
#pragma mark -
#pragma mark scrollviewdelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// NSLog(@"scrollview === %@",scrollView);
_leftTable.contentOffset = scrollView.contentOffset;
_centerTable.contentOffset = scrollView.contentOffset;
_rightTable.contentOffset = scrollView.contentOffset;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//在TableViewCell.h文件中定义waterFallImage的属性变量
@interface TableViewCell : UITableViewCell
@property (nonatomic,retain) UIImageView *waterFallImage;
@end
//在TableViewCell.m文件中设置每个row的规格
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.waterFallImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320/3, 200)];
[self.contentView addSubview:_waterFallImage];
[_waterFallImage release];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
NSLog(@"%s",__func__);
CGSize size = self.waterFallImage.image.size;
//设置waterFallImage的坐标,宽,高
CGFloat scale = size.width/(320/3);
CGFloat height = size.height/scale;
[self.waterFallImage setFrame:CGRectMake(0, 0, 320/3, height)];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end