设置组的头视图标题和尾视图标题【表视图的二维数组】
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;
}
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;
}
相关文章推荐
- adf开发出的table在IE8上面设置兼容性视图,导致显示标题和数据错位
- ios移动开发navigationItem自带的返回按钮设置成自定义图片或者不显示父视图的标题文字
- ipad UISplitViewController 导航视图控制器标题设置
- android 设置视图标题方法 requestWindowFeature()的应用
- 给collectionView设置头标题视图:
- vc 多文档 视图类设置标题
- 给collectionView设置头标题视图
- 设置标题和图标,设置窗口大小,设置光标
- winform中datagrid和datagridview设置列标题的实现
- ASPNETDB 表和视图(3) 个性化用户配置、页面个性化设置表和视图(转载)
- 使用了母版页的页面标题设置
- Eclipse RCP中设置视图标题栏的弧线形外观
- NeHe_Lesson4 视图变换矩阵的设置(旋转)
- Firefox - 附加软件 - Firebug - Script脚本视图 - 在页面加载过程中的脚本设置断点
- Rcp应该程序开发功能点——视图和编辑器的标题样式
- C# DataGrid设置列标题列宽代码
- C# WinForm中DataGrid列设置(列宽/列标题等)
- 动态设置页面标题(转)
- ASP.NET动态设置页面标题
- 嵌入视图设置的说明