(转)UITableView使用详解
2015-06-03 15:57
363 查看
NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多。不必再去建全局变量section和row。
NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
// 调整边距,可以让表格视图让开状态栏
self.tableView.contentInset
= UIEdgeInsetsMake(20,0,0,0);
1. UITableView的初始化
/** 隐藏状态栏 */-
(BOOL)prefersStatusBarHidden{ return YES;} 每个控制器管理
UITableView tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
[tableview setDelegate:self];
[tableview setDataSource:self];
[self.view addSubview: tableview];
(1) 在初始化UITableView的时候必须实现UITableView的是,在.h文件中要继承UITableViewDelegate和 UITableViewDataSource,并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承 UITableViewController实现的方法。
(2) 直接在XCODE生成项目的时候继承UITableViewController的,它会帮你自动写好UITableView必须要实现的方法。
(3)UITableView继承自UIScrollView。
2. UITableView的数据源
(1)UITableView是依赖外部资源为新表格单元填上内容的,我们称为数据源,这个数据源可以根据索引路径提供表格单元格,在UITableView中,索引路径是NSIndexPath的对象,可以选择分段或者分行,即是我们编码中的section和row。
(2)UITableView有三个必须实现的核心方法,分别如下:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
这个方法可以分段显示或者单个列表显示我们的数据.
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。
3. UITableView的委托方法
使用委托是为了响应用户的交互动作,比如下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供我们选择。
(1) 委托方法讲解
//设置Section的数量 索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return TitleData;
}
//设置每个section显示的Title
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{
return @"Andy-清风";
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
//设置每行调用的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
//UITableViewCellStyleDefault 默认类型 标题+可选图像
UITableViewCellStyleValue1 标题+明细+图像
UITableViewCellStyleValue2 不显示图像,标题+明细
UITableViewCellStyleSubtitle 标题+明细+图像
if (cell == nil) { cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier]
}
cell.imageView.image=image;//未选cell时的图片
cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
cell.text=@”Andy-清风”;
return cell;
}
//设置让UITableView行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}
//设置cell每行间隔的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
//设置UITableView的style
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
//设置选中Cell的响应事件
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}
//设置选中的行所执行的动作
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
return indexPath;
}
//设置划动cell是否出现del按钮,可供删除数据里进行处理
通过上面两步就实现了数据展示工作,接下就实现关键的数据删除了.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataArray removeObjectAtIndex:indexPath.row];
// Delete the row from the data source.
[testTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData]; //重新刷新
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
[self.tableView
insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}
(2) 其他
//选中cell时的颜色,在官方文档有如下可以选择
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
//cell右边按钮格式
typedef enum {
UITableViewCellAccessoryNone, //don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, //regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks
UITableViewCellAccessoryCheckmark //checkmark. doesn't track
} UITableViewCellAccessoryType
//是否加换行线
typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle
//改变换行线颜色
tableView.separatorColor= [UIColor blueColor];
4. UITableViewCell
表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。
UITableViewCell为每个Cell提供了三个可以选择的属性,如下:
l textLabel:填写文本
l detailTextLable:稍微详细的副标题
l imageView:用来显示你cell的图片,可以通过UIImage来加载。
最后给出一个官方的demo给大家学习下,多实践,不懂的就问下,下节课讲些UITableView应用中实际会出现的问题,比如自定义啊,重用单元格,单元格的数据排序等问题。欢迎大家拍砖。
NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
// 调整边距,可以让表格视图让开状态栏
self.tableView.contentInset
= UIEdgeInsetsMake(20,0,0,0);
1. UITableView的初始化
/** 隐藏状态栏 */-
(BOOL)prefersStatusBarHidden{ return YES;} 每个控制器管理
UITableView tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
[tableview setDelegate:self];
[tableview setDataSource:self];
[self.view addSubview: tableview];
(1) 在初始化UITableView的时候必须实现UITableView的是,在.h文件中要继承UITableViewDelegate和 UITableViewDataSource,并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承 UITableViewController实现的方法。
(2) 直接在XCODE生成项目的时候继承UITableViewController的,它会帮你自动写好UITableView必须要实现的方法。
(3)UITableView继承自UIScrollView。
2. UITableView的数据源
(1)UITableView是依赖外部资源为新表格单元填上内容的,我们称为数据源,这个数据源可以根据索引路径提供表格单元格,在UITableView中,索引路径是NSIndexPath的对象,可以选择分段或者分行,即是我们编码中的section和row。
(2)UITableView有三个必须实现的核心方法,分别如下:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
这个方法可以分段显示或者单个列表显示我们的数据.
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。
3. UITableView的委托方法
使用委托是为了响应用户的交互动作,比如下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供我们选择。
(1) 委托方法讲解
//设置Section的数量 索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return TitleData;
}
//设置每个section显示的Title
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{
return @"Andy-清风";
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
//设置每行调用的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
//UITableViewCellStyleDefault 默认类型 标题+可选图像
UITableViewCellStyleValue1 标题+明细+图像
UITableViewCellStyleValue2 不显示图像,标题+明细
UITableViewCellStyleSubtitle 标题+明细+图像
if (cell == nil) { cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier]
}
cell.imageView.image=image;//未选cell时的图片
cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
cell.text=@”Andy-清风”;
return cell;
}
//设置让UITableView行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}
//设置cell每行间隔的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
//设置UITableView的style
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
//设置选中Cell的响应事件
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}
//设置选中的行所执行的动作
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
return indexPath;
}
//设置划动cell是否出现del按钮,可供删除数据里进行处理
通过上面两步就实现了数据展示工作,接下就实现关键的数据删除了.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataArray removeObjectAtIndex:indexPath.row];
// Delete the row from the data source.
[testTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData]; //重新刷新
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
[self.tableView
insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}
(2) 其他
//选中cell时的颜色,在官方文档有如下可以选择
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
//cell右边按钮格式
typedef enum {
UITableViewCellAccessoryNone, //don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, //regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks
UITableViewCellAccessoryCheckmark //checkmark. doesn't track
} UITableViewCellAccessoryType
//是否加换行线
typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle
//改变换行线颜色
tableView.separatorColor= [UIColor blueColor];
4. UITableViewCell
表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。
UITableViewCell为每个Cell提供了三个可以选择的属性,如下:
l textLabel:填写文本
l detailTextLable:稍微详细的副标题
l imageView:用来显示你cell的图片,可以通过UIImage来加载。
最后给出一个官方的demo给大家学习下,多实践,不懂的就问下,下节课讲些UITableView应用中实际会出现的问题,比如自定义啊,重用单元格,单元格的数据排序等问题。欢迎大家拍砖。
相关文章推荐
- iOS UIView layer动画 总结(多谢前辈们分享)
- UVA 11248 Frequency Hopping 最小割
- UIWindow介绍
- 如何把图片UIImageView某个区域变成透明
- java、C++-带标签的break 和 continue语句
- UITableView 横向滚动(二)
- DRUID连接池的使用
- ueditor富文本
- ios UIImage修改大小后图片模糊解决办法
- iOS 全局修改UINavigation 后退按钮
- 动态修改UINavigationBar的背景色
- IOS UIFont 字体大全
- android UI 操作 不要在子线程中操作UI
- IOS自定义UIAlertView样式,实现可替换背景和按钮
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- Druid数据库连接池使用
- poj2299 Ultra-QuickSort
- heratbeat V2版配置LAMP HA说明及hb_gui工具使用说明
- 3个UIimageView实现图片的循环切换
- expdp query及parfile的使用