【理解】iOS开发之UITableView详解
2015-12-01 11:09
573 查看
表示图可谓是iOS开发中最常用的一个控件,没有之一。深入学习之后你会发现他有很多强大的功能,几乎可以用tableView实现所有的UI界面。
//表示图的创建
self.tableView=[[UITableViewalloc]initWithFrame:self.view.frame
style:UITableViewStylePlain];
//设置代理
self.tableView.delegate =self;
self.tableView.dataSource =self;
//设置背景颜色
self.tableView.backgroundColor = [UIColorredColor];
//分割线样式
self.tableView.separatorStyle
= UITableViewCellSeparatorStyleSingleLine;
//分割线颜色
self.tableView.separatorColor = [UIColorblueColor];
//行高
self.tableView.rowHeight =80;
//表示图的一些常用代理方法和数据源方法
#pragma mark ---------- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击方法!!!");
}
//返回每一行cell的高度,要比设置rowHeight更灵活
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {
return 66;
} else if (indexPath.section ==1) {
return 88;
}
return 44;
}
//头部分区高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
//尾部分区高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
//自定义分区头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headView = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,
self.frame.size.width,44)];
headView.backgroundColor = [UIColorgreenColor];
return headView;
}
//自定义分区尾部
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footView = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,
self.frame.size.width,44)];
footView.backgroundColor = [UIColoryellowColor];
return footView;
}
#pragma mark ---------- UITableViewDataSource
#pragma mark ----------
必须实现的代理方法
//返回分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
}
//INSERT CODE
cell.textLabel.text =@"iOS开发";
return cell;
}
#pragma mark ----------
选择实现的代理方法
//显示有几个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//分区头部标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section ==
0) {
return @"第一段标题";
} else if (section ==1) {
return @"第二段标题";
}
return@"段头标题";
}
//分区尾部标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section ==
0) {
return @"第一段段尾";
} else if (section ==1) {
return@"第二段段尾";
}
return@"段尾标题";
}
//右侧索引内容
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSArray *array =
@[@"k",@"z",@"s"];
return array;
}
//表示图的创建
self.tableView=[[UITableViewalloc]initWithFrame:self.view.frame
style:UITableViewStylePlain];
//设置代理
self.tableView.delegate =self;
self.tableView.dataSource =self;
//设置背景颜色
self.tableView.backgroundColor = [UIColorredColor];
//分割线样式
self.tableView.separatorStyle
= UITableViewCellSeparatorStyleSingleLine;
//分割线颜色
self.tableView.separatorColor = [UIColorblueColor];
//行高
self.tableView.rowHeight =80;
//表示图的一些常用代理方法和数据源方法
#pragma mark ---------- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击方法!!!");
}
//返回每一行cell的高度,要比设置rowHeight更灵活
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {
return 66;
} else if (indexPath.section ==1) {
return 88;
}
return 44;
}
//头部分区高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
//尾部分区高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
//自定义分区头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headView = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,
self.frame.size.width,44)];
headView.backgroundColor = [UIColorgreenColor];
return headView;
}
//自定义分区尾部
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footView = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,
self.frame.size.width,44)];
footView.backgroundColor = [UIColoryellowColor];
return footView;
}
#pragma mark ---------- UITableViewDataSource
#pragma mark ----------
必须实现的代理方法
//返回分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
}
//INSERT CODE
cell.textLabel.text =@"iOS开发";
return cell;
}
#pragma mark ----------
选择实现的代理方法
//显示有几个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//分区头部标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section ==
0) {
return @"第一段标题";
} else if (section ==1) {
return @"第二段标题";
}
return@"段头标题";
}
//分区尾部标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section ==
0) {
return @"第一段段尾";
} else if (section ==1) {
return@"第二段段尾";
}
return@"段尾标题";
}
//右侧索引内容
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSArray *array =
@[@"k",@"z",@"s"];
return array;
}
相关文章推荐
- 关于UIImageView的显示问题
- easyui tab 关闭
- 控制UI布局的方式
- Android uiautomator dump 命令介绍
- sample questions
- Installation failed due to invalid APK file!
- 从客户端(xxxxxxxxxxxxxxxxxxxxxx)中检测到有潜在危险的 Request.Form 值。
- UI考试
- UE4模型资源导出
- UE4组件
- [LeetCode]Longest Consecutive Sequence
- IOS入门——使用UIView简单实现霓虹灯(两种方法)
- [LeetCode]Binary Tree Longest Consecutive Sequence
- 3D touch 之 Home Screen Quick Action 的创建
- LeetCode -- Unique Paths II
- leetcode -- Unique Binary Search Trees II
- iOS 解决xib加载UITableViewCell的宽度不适应问题
- BlueprintListener的使用
- 【Qt Warning】No1:Debug build
- rabbitmq-queueDeclarePassive