AppDelegate设置根视图控制器
RootViewController.h
@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSArray *_data;
}
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//创建表视图
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
[tableView release];
<span style="color:#ff0000;">//读取本地的数据</span>
NSString *path = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"];
_data = [[NSArray alloc] initWithContentsOfFile:path];
}
#pragma mark - UITableView dataSource
//指定组的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _data.count;
}
//指定相应组的cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *arrary2D = [_data objectAtIndex:section];
return arrary2D.count;
}
//创建单元格cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSArray *arrary2D = [_data objectAtIndex:indexPath.section];
NSString *str = [arrary2D objectAtIndex:indexPath.row];
cell.textLabel.text = str;
cell.textLabel.font = [UIFont fontWithName:str size:17];
return [cell autorelease];
}
#pragma mark - UITableViewDelegate
//单元格被选中后调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d组%d行的单元格被选中了",indexPath.section,indexPath.row);
<span style="color:#ff0000;">//取消选中效果</span>
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/*
下面这个方法
*/
//给组的头视图设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *str = [NSString stringWithFormat:@"头视图的标题:%d组的标题",section];
return str;
}
//给组的尾视图设置标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
NSString *str = [NSString stringWithFormat:@"尾视图的标题:%d组的标题",section];
return str;
}
//自定义组的头视图和尾视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
<span style="background-color: rgb(255, 0, 0);"> //headerView的frame值设置没有效果</span>
//CGRectMake(0, 0, 0, 0)等价于CGRectZero
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectZero]autorelease];
headerView.backgroundColor = [UIColor redColor];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 10, 180, 20)]autorelease];
label.text = [NSString stringWithFormat:@"尾视图的标题:%d组的标题",section];
label.textColor = [UIColor greenColor];
label.font = [UIFont systemFontOfSize:15];
[headerView addSubview:label];
return headerView;
}
//设置组的头视图的高度
//或者 tableView.sectionHeaderHeight
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}