要求:在一個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
本文轉蓬萊仙羽51CTO博客,原文鏈接:http://blog.51cto.com/dingxiaowei/1366560,如需轉載請自行聯系原作者