UITableView中的DataSource和delegate
2016-04-22 11:00
507 查看
1,顾名思义,dataSource意思为数据源,delegate意为代理,其内部包含很多方法。
UITableView需要显示数据,则数据源(datasource)可以给他提供数据从而显示出来,其会向数据源查询一共有多少行数据以及显示什么数据等,如果没有设置数据源的,那么UITableView就不会产生任何数据,没有作用。同时,遵守UITableViewDataSource协议的都可以是数据源,所以添加协议从而提供数据源。
同时UITableView也要设置对象,以防在UITableView触发事件时可以做出处理,比如选中某一行和某一列。遵守UITableViewDataSource协议的都可以是代理对象。
2,上边提到的UITableViewDataSource 协议中常用方法:
(1.)设置右边索引值
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
(2.)设置分组标识
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
(3.)设置分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
(4.)设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(5.)创建cell(使用重用机制,如下例)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
(6.)设置tableView的每一行的编辑状态(YES,可编辑)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES
}
(7.)edit按钮的点击事件(当点击edit按钮时触发)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
(8.)当提交编辑操作时触发
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
(9.)设置tableView每一行是否允许移动(YES,可移动)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES
}
(10.)提交移动操作之后触发
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
3,UITableViewDelegate协议中常用方法
(1).设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
(2).选中cell时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
(3.)设置tableViewCell的编辑样式(插入/删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
(4.)设置当点击编辑按钮时上面显示的文字,如显示删除
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) { return
@"删除"; }
(5).设置cell移动的位置
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
UITableView需要显示数据,则数据源(datasource)可以给他提供数据从而显示出来,其会向数据源查询一共有多少行数据以及显示什么数据等,如果没有设置数据源的,那么UITableView就不会产生任何数据,没有作用。同时,遵守UITableViewDataSource协议的都可以是数据源,所以添加协议从而提供数据源。
同时UITableView也要设置对象,以防在UITableView触发事件时可以做出处理,比如选中某一行和某一列。遵守UITableViewDataSource协议的都可以是代理对象。
2,上边提到的UITableViewDataSource 协议中常用方法:
(1.)设置右边索引值
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
(2.)设置分组标识
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
(3.)设置分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
(4.)设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(5.)创建cell(使用重用机制,如下例)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
(6.)设置tableView的每一行的编辑状态(YES,可编辑)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES
}
(7.)edit按钮的点击事件(当点击edit按钮时触发)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
(8.)当提交编辑操作时触发
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
(9.)设置tableView每一行是否允许移动(YES,可移动)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES
}
(10.)提交移动操作之后触发
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
3,UITableViewDelegate协议中常用方法
(1).设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
(2).选中cell时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
(3.)设置tableViewCell的编辑样式(插入/删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
(4.)设置当点击编辑按钮时上面显示的文字,如显示删除
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) { return
@"删除"; }
(5).设置cell移动的位置
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
相关文章推荐
- 获取iPhone各个版本屏幕大小
- ios多线程编程 - NSOperationQueue
- iOS 中UIView contentMode 属性总结
- VUE中的v-if与v-show
- android stduio 获取sha1
- MySQL分析工具之pt-query-digest
- JSP使用jstl标签报错,jsp页面提示找不到build path
- Daikon Forge GUI Library(dfgui)之Event Binding
- UI实时动态设计技巧
- Ui的基本特效--2
- iOS 本地通知 UILocationNotification
- 动效设计原理:从卡通动画到UI动效
- UI的基本动效--1
- OrangeUI将支持皮肤包的功能
- Android 5.0 Material 自定义Dialog 背景 字体大小颜色 ui组件颜色
- UI for iOS (三) - TableView for iOS
- Kendo UI常用示例汇总(六)
- 沫沫金:EasyUI 固定列
- 【创建型模式】建造者模式(Builder)之23种java设计模式
- EasyUI 增删改查完全详细