您的位置:首页 > 其它

设置组的头视图标题和尾视图标题【表视图的二维数组】

2014-10-30 09:48 288 查看
AppDelegate.m设置根控制器

RootViewController.h

@interface RootViewController : UIViewController<UITableViewDataSource>
{
NSArray *_data;
}
RootViewController.m
- (void)dealloc
{

[_data release];

[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];

//创建表视图
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStyleGrouped];
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView release];

//二维数组
/*
[@"字体1",@"字体2",@"字体3",@"字体4"],
[@"字体1",@"字体2",@"字体3"],
[@"字体1",@"字体4"]
*/

//在程序报中查找文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"];

_data = [[NSArray alloc] initWithContentsOfFile:path];
}

#pragma mark - UITableView dataSource

//指定组的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return _data.count;

}

//指定行的个数()多少个单元格
//有多少组执行多少次
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSArray *arrary2D = [_data objectAtIndex:section];
return arrary2D.count;
}

//创建单元格
/*
[@"字体1",@"字体2",@"字体3",@"字体4"],
[@"字体1",@"字体2",@"字体3"],
[@"字体1",@"字体4"]
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

//创建单元格
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
//组的索引
NSInteger section = indexPath.section;//组
NSInteger row = indexPath.row;//行
//组的元素内容
NSArray *arrary2D = [_data objectAtIndex:section];
//行的元素内容
NSString *name = [arrary2D objectAtIndex:row];
//填充单元
cell.textLabel.text = name;
//设置单元格字体
cell.textLabel.font = [UIFont fontWithName:name size:17];

return [cell autorelease];

}

//设置组的头视图标题
- (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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息