总结
本章主要简示了UITabView得使用方法,使用UITabViewController的三个页面演示不同情况,包括简单的表格,分段带副标题风格的表格以及索引表格。
运行结果
工程构建概要
1.UITabViewController的使用方法可参考以前的文章
2.使用UITabView的控制类必须实现该控件对应的数据源和代理的方法,同时需要在IB里面指定UITabView的数据源和代理为该界面的控制类。
3.主要实现的方法有有几个段,每个段的行数,每个单元格怎么绘制,段标题是什么,如果需要索引的话,还得实现索引标题,点击索引标题定位到段。
4.因为本工程没有使用ARC,所以一开始有内存管理的问题,主要是成员变量赋值的时候是浅拷贝。正确的使用方法是self._value = value,而不是_vale = value,前者会调用设置方法,而后者只是指针赋值。
主要代码及注释
h文件
//
// IndexViewController.h
// TableViewDemo
//
// Created by arbboter on 14/12/5.
// Copyright (c) 2014年 arbboter. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface IndexViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSMutableArray* _arraySection;
NSMutableArray* _arrayMember;
NSArray* _arrayIndex;
}
@property (nonatomic, retain) NSMutableArray* _arraySection;
@property (nonatomic, retain) NSMutableArray* _arrayMember;
@property (nonatomic, retain) NSArray* _arrayIndex;
@end
m文件
//
// IndexViewController.m
// TableViewDemo
//
// Created by arbboter on 14/12/5.
// Copyright (c) 2014年 arbboter. All rights reserved.
//
#import "IndexViewController.h"
@implementation IndexViewController
@synthesize _arrayMember;
@synthesize _arraySection;
@synthesize _arrayIndex;
#pragma UITableViewDataSource<NSObject>
// 对应段的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger ret = 0;
ret = [[_arraySection objectAtIndex:section] count];
return ret;
}
// 绘制单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
static NSString *CellIdentifier = @"Cell1";
cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[_arraySection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [_arrayIndex objectAtIndex:indexPath.section]]];
return cell;
}
// 索引标题列表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray* index = [[NSMutableArray alloc] init];
for (int i='0'; i<='9'; i++)
{
[index addObject:[NSString stringWithFormat:@"%c", i]];
}
for (int i='A'; i<='Z'; i++)
{
[index addObject:[NSString stringWithFormat:@"%c", i]];
}
self._arrayIndex = index;
[index release];
return _arrayIndex;
}
// 点击索引标题定位到段
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
// 分段数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return [_arraySection count];
}
// 返回段标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_arrayIndex objectAtIndex:section];
}
#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* strRow = nil;
NSArray* array = [_arraySection objectAtIndex:indexPath.section];
strRow = [array objectAtIndex:indexPath.row];
NSString* msg = [[NSString alloc] initWithFormat:@"你选择了-%@", strRow];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[msg release];
[alert release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSMutableArray* arrayIndex = [[NSMutableArray alloc] init];
// 数字名
for (char i='0'; i<='9'; i++)
{
int nMem = arc4random()%7+4;
NSMutableArray* arrayMem = [[NSMutableArray alloc] init];
for (int j=0; j<nMem; j++)
{
[arrayMem addObject:[NSString stringWithFormat:@"%c%c%c", i, arc4random()%10+'0', arc4random()%10+'0']];
}
[arrayIndex addObject:arrayMem];
}
// 字母名
for (char i='A'; i<='Z'; i++)
{
int nMem = arc4random()%7+4;
NSMutableArray* arrayMem = [[NSMutableArray alloc] init];
for (int j=0; j<nMem; j++)
{
[arrayMem addObject:[NSString stringWithFormat:@"%c%c%c", i, arc4random()%26+'a', arc4random()%26+'a']];
}
[arrayIndex addObject:arrayMem];
}
self._arraySection = arrayIndex;
self._arrayMember = [arrayIndex objectAtIndex:0];
[arrayIndex release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
for (int i=0; i<_arraySection.count; i++)
{
[[_arraySection objectAtIndex:i] release];
}
[_arraySection release];
[_arrayMember release];
[super dealloc];
}
@end