UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示。下面以一个简单的实例来介绍tableview的基本用法。(适合新手,高手飘过)
[cpp] view plain copy
@interface TableViewTestViewController : UIViewController
UITableView *DataTable;
NSMutableArray *dataArray1; //定义数据数组1
NSMutableArray *dataArray2;//定义数据数组2
NSMutableArray *titleArray;//定义标题数组
}
- (void)viewDidLoad
{
[superviewDidLoad];
//初始化tableview
DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小
[DataTablesetDelegate:self];//指定委托
[DataTablesetDataSource:self];//指定数据委托
[self.viewaddSubview:DataTable];//加载tableview
dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中国", @"美国", @"英国", nil];//初始化数据数组1
dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黄种人", @"黑种人", @"白种人", nil];//初始化数据数组2
titleArray = [[NSMutableArrayalloc] initWithObjects:@"国家", @"种族", nil];//初始化标题数组
}
- ( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题
case 1:
return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题
default:
return @"Unknown";
}
附上出处链接:http://www.tuicool.com/articles/eAzqUr