ios-表视图创建
2016-07-21 17:35
525 查看
1、创建一个表视图的两种样式
UITableViewStylePlain -----> 平铺的效果
UITableViewStyleGrouped -----> 分组的效果
2、表视图的结构
a) 表视图由头部是视图,尾部视图,中间由一连串的单元格组成
b) 表视图的头部由tableHeaderView属性设置,尾部由tableFooterView属性设置
c) 分组表格由一连串的section视图组成
.m文件中
#import "ViewController.h"
//1.
实现UITableViewDataSource协议,如果不实现,就无法加载数据
@interface ViewController () <UITableViewDataSource>
@property(nonatomic, strong) NSArray *fNames;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 200, 500) style:UITableViewStylePlain];
// 设置数据源代理:如果不设置,数据就无法显示
tableView.dataSource = self;
// 添加到视图中
[self.view addSubview:tableView];
// 初始化字体数组
_fNames = [UIFont familyNames];
}
#pragma mark -
返回单元格的分组个数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
#pragma mark -
必须实现的数据协议
//2.
返回分组的单元格个数
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
//返回的单元格的个数 ---->
字体的个数
return _fNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//1)
创建一个单元格
/**
UITableViewCellStyleDefault //默认
UITableViewCellStyleValue1 //字体改变
UITableViewCellStyleValue2 //蓝色字体
UITableViewCellStyleSubtitle //自适应
*/
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//2)
设置单元格的内容:indexPath.section ------>
代表分组
// indexPath.row -------->
代表行
cell.textLabel.text = [NSString stringWithFormat:@"组:%ld,
行:%ld", indexPath.section, indexPath.row];
//3)
设置字体内容
cell.textLabel.text = _fontNames[indexPath.row];
//4)
设置字体样式
cell.textLabel.font = [UIFont fontWithName:_fontNames[indexPath.row] size:20];
return cell;
}
UITableViewStylePlain -----> 平铺的效果
UITableViewStyleGrouped -----> 分组的效果
2、表视图的结构
a) 表视图由头部是视图,尾部视图,中间由一连串的单元格组成
b) 表视图的头部由tableHeaderView属性设置,尾部由tableFooterView属性设置
c) 分组表格由一连串的section视图组成
.m文件中
#import "ViewController.h"
//1.
实现UITableViewDataSource协议,如果不实现,就无法加载数据
@interface ViewController () <UITableViewDataSource>
@property(nonatomic, strong) NSArray *fNames;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 200, 500) style:UITableViewStylePlain];
// 设置数据源代理:如果不设置,数据就无法显示
tableView.dataSource = self;
// 添加到视图中
[self.view addSubview:tableView];
// 初始化字体数组
_fNames = [UIFont familyNames];
}
#pragma mark -
返回单元格的分组个数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
#pragma mark -
必须实现的数据协议
//2.
返回分组的单元格个数
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
//返回的单元格的个数 ---->
字体的个数
return _fNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//1)
创建一个单元格
/**
UITableViewCellStyleDefault //默认
UITableViewCellStyleValue1 //字体改变
UITableViewCellStyleValue2 //蓝色字体
UITableViewCellStyleSubtitle //自适应
*/
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//2)
设置单元格的内容:indexPath.section ------>
代表分组
// indexPath.row -------->
代表行
cell.textLabel.text = [NSString stringWithFormat:@"组:%ld,
行:%ld", indexPath.section, indexPath.row];
//3)
设置字体内容
cell.textLabel.text = _fontNames[indexPath.row];
//4)
设置字体样式
cell.textLabel.font = [UIFont fontWithName:_fontNames[indexPath.row] size:20];
return cell;
}
相关文章推荐
- GUI - Web前端开发框架
- 评价ui设计作品好坏的八个标准(界面/交互设计研究)
- Jquery ui datepicker设置日期范围,如只能隔3天【实现代码】
- 为Yahoo! UI Extensions Grid增加内置的可编辑器
- Cocos2d-x UI开发之文本类使用实例
- 谈谈对jquery ui tabs 的理解
- 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
- UI Events 用户界面事件
- 基于Bootstrap的UI扩展 StyleBootstrap
- jQuery UI结合Ajax创建可定制的Web界面
- jquery UI Datepicker时间控件的使用方法(基础版)
- ui组件之input多选下拉实现方法(带有搜索功能)
- Hallo.js基于jQuery UI所见即所得的Web编辑器
- 基于jQuery UI CSS Framework开发Widget的经验
- jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭)
- jquery UI Datepicker时间控件的使用及问题解决
- jQuery UI设置固定日期选择特效代码分享
- Jquery ajax 同步阻塞引起的UI线程阻塞问题
- jQuery UI的Dialog无法提交问题的解决方法
- jQuery ui实现动感的圆角渐变网站导航菜单效果代码