IOS開發(fā)筆記 多個(gè)tableView的使用
UITableView是app開發(fā)中常用到的控件,功能很強(qiáng)大,多用于數(shù)據(jù)的顯示。下面以一個(gè)簡(jiǎn)單的實(shí)例來介紹tableview的基本用法。(適合新手,高手飄過)
[cpp] view plaincopy
- @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
- UITableView *DataTable;
- NSMutableArray *dataArray1; //定義數(shù)據(jù)數(shù)組1
- NSMutableArray *dataArray2;//定義數(shù)據(jù)數(shù)組2
- NSMutableArray *titleArray;//定義標(biāo)題數(shù)組
- }
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- //初始化tableview
- DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小
- [DataTablesetDelegate:self];//指定委托
- [DataTablesetDataSource:self];//指定數(shù)據(jù)委托
- [self.viewaddSubview:DataTable];//加載tableview
- dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國", @"美國", @"英國", nil];//初始化數(shù)據(jù)數(shù)組1
- dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];//初始化數(shù)據(jù)數(shù)組2
- titleArray = [[NSMutableArrayalloc] initWithObjects:@"國家", @"種族", nil];//初始化標(biāo)題數(shù)組
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- //每個(gè)section顯示的標(biāo)題
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- switch (section) {
- case 0:
- return [titleArray objectAtIndex:section];//提取標(biāo)題數(shù)組的元素用來顯示標(biāo)題
- case 1:
- return [titleArray objectAtIndex:section];//提取標(biāo)題數(shù)組的元素用來顯示標(biāo)題
- default:
- return @"Unknown";
- }
- }
- //指定有多少個(gè)分區(qū)(Section),默認(rèn)為1
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return [titleArray count];//返回標(biāo)題數(shù)組中元素的個(gè)數(shù)來確定分區(qū)的個(gè)數(shù)
- }
- //指定每個(gè)分區(qū)中有多少行,默認(rèn)為1
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- switch (section) {
- case 0:
- return [dataArray1 count];//每個(gè)分區(qū)通常對(duì)應(yīng)不同的數(shù)組,返回其元素個(gè)數(shù)來確定分區(qū)的行數(shù)
- break;
- case 1:
- return [dataArray2 count];
- break;
- default:
- return 0;
- break;
- }
- }
- //繪制Cell
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- //初始化cell并指定其類型,也可自定義cell
- UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if(cell == nil)
- {
- cell = [[[UITableViewCellalloc]
- initWithFrame:CGRectZero
- reuseIdentifier:CellIdentifier] autorelease];
- }
- switch (indexPath.section) {
- case 0://對(duì)應(yīng)各自的分區(qū)
- [[cell textLabel] setText:[dataArray1 objectAtIndex:indexPath.row]];//給cell添加數(shù)據(jù)
- break;
- case 1:
- [[cell textLabel] setText:[dataArray2 objectAtIndex:indexPath.row]];
- break;
- default:
- [[cell textLabel] setText:@"Unknown"];
- }
- return cell;//返回cell
- }
上面的例子在功能上介紹了tableview的使用,但其在數(shù)據(jù)處理上具有很大的局限性。當(dāng)我們要從服務(wù)器上請(qǐng)求數(shù)據(jù),面對(duì)多種可能的數(shù)據(jù)(主要指數(shù)組的個(gè)數(shù)不穩(wěn)定)此時(shí)上面的switch將無法滿足我們的需求了。使用switch可是代碼的結(jié)構(gòu)清晰明了,但其局限性很致命(switch中case的個(gè)數(shù)無法動(dòng)態(tài)指定),下面的另一種方法可解決上述問題。
代碼在原由基礎(chǔ)上進(jìn)行的修改:
[cpp] view plaincopy
- @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
- UITableView *DataTable;
- NSMutableArray *dataArray1;
- NSMutableArray *dataArray2;
- NSMutableArray *titleArray;
- NSMutableArray *dataArray; //加入了用于保存數(shù)組的數(shù)組 dataArray
- }
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
- [DataTablesetDelegate:self];
- [DataTablesetDataSource:self];
- [self.viewaddSubview:DataTable];
- dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國", @"美國", @"英國", nil];
- dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];
- titleArray = [[NSMutableArrayalloc] initWithObjects:@"國家", @"種族", nil];
- dataArray = [[NSMutableArrayalloc] initWithObjects:dataArray1, dataArray2, nil]; //初始化dataArray,元素為數(shù)組
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- //制定個(gè)性標(biāo)題,這里通過UIview來設(shè)計(jì)標(biāo)題,功能上豐富,變化多。
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- UIView *view = [[[UIViewalloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
- [view setBackgroundColor:[UIColorbrownColor]];//改變標(biāo)題的顏色,也可用圖片
- UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
- label.textColor = [UIColorredColor];
- label.backgroundColor = [UIColorclearColor];
- label.text = [titleArrayobjectAtIndex:section];
- [view addSubview:label];
- return view;
- }
- //指定標(biāo)題的高度
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
- return 40;
- }
- //每個(gè)section顯示的標(biāo)題,有了上面的這個(gè)就不要了
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- }
- //指定有多少個(gè)分區(qū)(Section),默認(rèn)為1
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return [titleArraycount];
- }
- //指定每個(gè)分區(qū)中有多少行,默認(rèn)為1
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- /* switch (section) {
- case 0:
- return [dataArray1 count];
- break;
- case 1:
- return [dataArray2 count];
- break;
- default:
- return 0;
- break;
- }*/
- /* for(int i = 0; i < [titleArray count]; i++){
- if(section == i){
- return [[dataArray objectAtIndex:section] count];
- }
- }*/
- ?。厦娴姆椒ㄒ彩强尚械模蠹覅⒖急容^下
- return [[dataArray objectAtIndex:section] count]; //取dataArray中的元素,并根據(jù)每個(gè)元素(數(shù)組)來判斷分區(qū)中的行數(shù)。
- }
- //繪制Cell
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = (UITableViewCell*)[tableView
- dequeueReusableCellWithIdentifier:CellIdentifier];
- if(cell == nil)
- {
- cell = [[[UITableViewCellalloc]
- initWithFrame:CGRectZero
- reuseIdentifier:CellIdentifier] autorelease];
- }
- /*switch (indexPath.section) {
- case 0:
- [[cell textLabel]
- setText:[dataArray1 objectAtIndex:indexPath.row]];
- break;
- case 1:
- [[cell textLabel]
- setText:[dataArray2 objectAtIndex:indexPath.row]];
- break;
- default:
- [[cell textLabel]
- setText:@"Unknown"];
- }*/
- //上面的方法也可行,大家比較下。
- [[cell textLabel] setText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
- //同上,取出dataArray中每個(gè)分區(qū)所對(duì)應(yīng)的元素(數(shù)組),并通過其來取值。 (大家要有想像力, 復(fù)制代碼試試就明白了)
- return cell;
- }
- //改變行的高度
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return40;
- }
轉(zhuǎn)自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html
一天,一個(gè)月,一年??傆幸惶鞎?huì)變得不一樣。
posted on 2014-08-20 10:44 wokaoJune 閱讀(2705) 評(píng)論(0) 編輯 收藏 所屬分類: IOS