8月19学习练习[两三个TableView并排显示]

简介: 要求:在一个view中显示两个tableView,要求左右显示的内容以及行数不一样,且左边每行显示两张图片(分别3个一轮回,2个一轮回)并且显示中国的城市名,右边显示水果名。

要求:在一个view中显示两个tableView,要求左右显示的内容以及行数不一样,且左边每行显示两张图片(分别3个一轮回,2个一轮回)并且显示中国的城市名,右边显示水果名。点击时分别显示城市名或水果名的对话框(偶数的城市不能点击)(所选的图片长短比例不一致)

运行图:


步骤:

1.创建一个tableCell.xib


2.viewController.h:

#import <UIKit/UIKit.h>
@interface DXWViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (retain, nonatomic) IBOutlet UITableViewCell *ccell;
@end

3.viewController.m:

//
//  DXWViewController.m
//  两个tableView
//
//  Created by 丁小未 on 13-8-19.
//  Copyright (c) 2013年 dingxiaowei. All rights reserved.
//

#import "DXWViewController.h"
#define heightofimage(image) image.size.height*150.0/image.size.width
@interface DXWViewController ()
{
    NSMutableArray *arrdata;
    NSMutableArray *arrdata1;
    NSMutableArray *arrdata2;
    NSMutableArray *arrdata2_1;
    NSMutableArray *arrdata3;
    float he1,he2;
}
@property (retain, nonatomic) IBOutlet UITableView *tableview1;
@property (retain, nonatomic) IBOutlet UITableView *tableview2;
@property (retain, nonatomic) IBOutlet UITableView *tableview3;

@end

@implementation DXWViewController

@synthesize tableview1,tableview2,tableview3;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Backhome) name:@"Back" object:nil];
    he1 = 0.0;
    he2 = 0.0;
    arrdata1 = [[NSMutableArray alloc] initWithCapacity:1];
    arrdata2 = [[NSMutableArray alloc] initWithCapacity:1];
    arrdata3 = [[NSMutableArray alloc] initWithObjects:@"苹果",@"香蕉",@"梨子",@"菠萝",@"桃子",@"西瓜",@"葡萄",@"凌檬",@"樱桃", @"芒果",nil];
    arrdata2_1 = [[NSMutableArray alloc] initWithObjects:@"江苏", @"南京",@"河北",@"武汉",@"南通",@"北京",@"合肥",@"天津",@"长沙",@"苏州",nil];
    for (int i=1; i<11; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg",i]];
        float hecu = image.size.height *150/image.size.width;
        
        if (he2>=he1) {
            he1 = he1 +hecu;
            NSArray *arr = [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"%d",i],[NSString stringWithFormat:@"%f",hecu], nil];
            [arrdata1 addObject:arr];
        }
        else
        {
            he2 = he2 + hecu;
            NSArray *arr = [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"%d",i],[NSString stringWithFormat:@"%f",hecu], nil];
            [arrdata2 addObject:arr];
        }
    }
    tableview1.showsVerticalScrollIndicator = NO;
    tableview2.showsVerticalScrollIndicator = NO;
    NSLog(@"%@+++++++%@",arrdata1,arrdata2);
}
//两个tableview显示的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == tableview1) {
        return [arrdata1 count];
    }else if(tableView == tableview2){
        return [arrdata2 count];
    }
    else
    {
        return [arrdata3 count]*4;
    }
    
    return 0;
}

//两个tableview显示的高度
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    if (tableView == tableview1) {
        return [[[arrdata1 objectAtIndex:row] objectAtIndex:1] floatValue] +10;
    }else if(tableView == tableview2){
        return [[[arrdata2 objectAtIndex:row] objectAtIndex:1] floatValue] +10;
    }
    else
    {
        return 44;
    }
}

//两个tableview一起滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == tableview1) {
        [tableview2 setContentOffset:tableview1.contentOffset];
    }
    else if(scrollView = tableview2){
        [tableview1 setContentOffset:tableview2.contentOffset];
    }
    
}

//tableviewcell的初始化
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    if (tableView == tableview1) {
        static NSString *id1 = @"sd1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id1];
        if (cell == nil) {
            NSArray *nib  = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:self options:nil];
            if (nib>0) {
                cell = _ccell;
            }
        }
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg",[[[arrdata1 objectAtIndex:row] objectAtIndex:0] integerValue]]];
        UIImageView *imageview = (UIImageView *)[cell viewWithTag:101];
        [imageview setImage:image];
        CGRect rect = imageview.frame;
        rect.size.height = [[[arrdata1 objectAtIndex:row] objectAtIndex:1] floatValue];
        imageview.frame = rect;
        return cell;
    }
    else if(tableView == tableview2)
    {
        static NSString *id = @"sd";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:self options:nil];
            if (nib > 0) {
                cell = _ccell;
            }
            //创建UILabel
            UILabel *label = [[UILabel alloc] init];
            label.text = [arrdata2_1 objectAtIndex:indexPath.row];
            label.frame = CGRectMake(10, 7, 100, 35);
            //将UILabel添加到contentView中
            [cell.contentView addSubview:label];
            
        }
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg",[[[arrdata2 objectAtIndex:row] objectAtIndex:0] integerValue]]];
        UIImageView *imageview = (UIImageView *)[cell viewWithTag:101];
        [imageview setImage:image];
        CGRect rect = imageview.frame;
        rect.size.height = [[[arrdata2 objectAtIndex:row] objectAtIndex:1] floatValue];
        imageview.frame = rect;
        return cell;
    }
    else if(tableView == tableview3)
    {
        NSString *identifier = @"aaa";//创建一个表示符
        //询问带这种标志的可以用的cell有没有
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        }
        int row = [indexPath row];//需要显示的是第几行
        cell.textLabel.text = [arrdata3 objectAtIndex:row%[arrdata3 count]];
        return cell;
    }
    return nil;
}

//点击某行的时候执行的操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == tableview3) {
        NSString * str= [arrdata3 objectAtIndex:[indexPath row]%[arrdata3 count]];
        UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"你选中的水果" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [a show];
        [a release];
        //取消选中
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

//将要选中某行时候做的事情,常用来判断某行是否能点击
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //偶数行不能点击
    if ([indexPath row]%2==1) {
        return nil;
    }
    return indexPath;
    
}

- (void)Backhome{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [self.ccell release];
    [self.tableview1 release];
    [self.tableview2 release];
    [self.tableview3 release];
    [super dealloc];
}

@end


相关文章
TableView列表展开不同cell
TableView列表展开不同cell
86 0
TableView列表展开不同cell
UITableView的创建
UITableView的创建
75 0
|
iOS开发
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
919 0
|
iOS开发 容器
(五)UITableView的用法一
(五)UITableView的用法一
160 0
|
存储 iOS开发 搜索推荐