1. 程式人生 > >【程式碼筆記】iOS-scrollerView裡多個tableView加搜尋框

【程式碼筆記】iOS-scrollerView裡多個tableView加搜尋框

#import "RootViewController.h" #import "customCell.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) {
// Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //初始化背景圖 [self initBackgroundView]; } -(void)initBackgroundView { self.navigationController.navigationBarHidden=YES; dataArray
= [[NSMutableArray alloc]initWithObjects:@"熱門微博",@"熱門話題",@"隨便看看",@"二維碼", @"周邊的微博",@"周邊的人",@"微吧",@"微博會員", @"新浪早晚報",@"微公益",@"微博應用",@"遊戲", @"微刊",@"手機微博",@"微群",@"微相簿", @"名人堂",@"人氣草根",@"微博精選",@"猜你喜歡", @"手機新浪網",@"掌中新浪",@"
天氣通",@"微漫畫", @"聊天",@"看點",@"微盤",@"微收藏", @"圖釘",@"限免換享",@"淘身邊",@"微美食", @"新浪拍客",@"部落格",@"充話費",@"MOMO島", @"網址導航",@"新浪視野",@"微資料",@"新浪彩票",nil]; imageArray = [[NSMutableArray alloc] initWithObjects:@"0.png",@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png",@"11.png",@"12.png",@"13.png",@"14.png",@"15.png",@"16.png",@"17.png",@"18.png",@"19.png",@"20.png",@"21.png",@"22.png",@"23.png",@"24.png",@"25.png",@"26.png",@"27.png",@"28.png",@"29.png",@"30.png",@"31.png",@"32.png",@"33.png",@"34.png",@"35.png",@"36.png",@"37.png",@"38.png",@"39.png", nil]; //scrollerView scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460-44-44)]; scrollerView.contentSize = CGSizeMake(320*4, 460-44-44); scrollerView.bounces = YES; scrollerView.tag = 101; scrollerView.showsHorizontalScrollIndicator = NO; scrollerView.pagingEnabled = YES; scrollerView.delegate = self; [self.view addSubview:scrollerView]; //tableOne tableOneView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 460-44-44-44) style:UITableViewStylePlain]; tableOneView.delegate = self; tableOneView.dataSource = self; tableOneView.bounces=NO; [tableOneView setSeparatorColor:[UIColor clearColor]]; //tableTwo tableTowView = [[UITableView alloc] initWithFrame:CGRectMake(320, 44, 320, 460-44-44-44) style:UITableViewStylePlain]; tableTowView.delegate = self; tableTowView.dataSource = self; tableTowView.bounces=NO; [tableTowView setSeparatorColor:[UIColor clearColor]]; //tableThree tableThreeView = [[UITableView alloc]initWithFrame:CGRectMake(320*2, 44, 320, 460-44-44-44) style:UITableViewStylePlain]; tableThreeView.delegate = self; tableThreeView.dataSource = self; tableThreeView.bounces=NO; [tableThreeView setSeparatorColor:[UIColor clearColor]]; //tableFour tableFourView = [[UITableView alloc] initWithFrame:CGRectMake(320*3, 44, 320, 460-44-44-44) style:UITableViewStylePlain]; tableFourView.delegate = self; tableFourView.dataSource = self; tableFourView.bounces=NO; [tableFourView setSeparatorColor:[UIColor clearColor]]; [scrollerView addSubview:tableFourView]; [scrollerView addSubview:tableThreeView]; [scrollerView addSubview:tableTowView]; [scrollerView addSubview:tableOneView]; //searchbar UISearchBar * searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; searchBar.placeholder = @"搜尋"; searchBar.delegate = self; searchBar.showsCancelButton = YES; [self.view addSubview:searchBar]; //searchcontroller scrollerControl = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self]; scrollerControl.delegate = self; scrollerControl.searchResultsDataSource = self; scrollerControl.searchResultsDelegate = self; } #pragma -mark -UITableViewDelegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static int i = 0; static int j = 0; NSString * strID = @"ID"; cell = [tableView dequeueReusableCellWithIdentifier:strID]; if (cell == nil) { cell = [[customCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strID]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.button1.tag = i++; cell.button2.tag = i++; cell.button3.tag = i++; cell.button4.tag = i++; cell.label1.tag = j++; cell.label2.tag = j++; cell.label3.tag = j++; cell.label4.tag = j++; if (cell.label1.tag >= 40 || cell.button1.tag >= 40) { return cell; } } [cell.label1 setText:[NSString stringWithFormat:@"%@",[dataArray objectAtIndex:cell.label1.tag]]]; [cell.label2 setText:[NSString stringWithFormat:@"%@",[dataArray objectAtIndex:cell.label2.tag]]]; [cell.label3 setText:[NSString stringWithFormat:@"%@",[dataArray objectAtIndex:cell.label3.tag]]]; [cell.label4 setText:[NSString stringWithFormat:@"%@",[dataArray objectAtIndex:cell.label4.tag]]]; [cell.button1 setImage:[UIImage imageNamed:[imageArray objectAtIndex:cell.button1.tag]] forState:UIControlStateNormal]; [cell.button2 setImage:[UIImage imageNamed:[imageArray objectAtIndex:cell.button2.tag]] forState:UIControlStateNormal]; [cell.button3 setImage:[UIImage imageNamed:[imageArray objectAtIndex:cell.button3.tag]] forState:UIControlStateNormal]; [cell.button4 setImage:[UIImage imageNamed:[imageArray objectAtIndex:cell.button4.tag]] forState:UIControlStateNormal]; [cell.button1 addTarget:self action:@selector(doClickButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.button2 addTarget:self action:@selector(doClickButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.button3 addTarget:self action:@selector(doClickButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.button4 addTarget:self action:@selector(doClickButtonAction:) forControlEvents:UIControlEventTouchUpInside]; return cell; } -(void)doClickButtonAction:(UIButton *)btn { NSLog(@"--btn.tag--%i",btn.tag); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }