TableView的属性和方法的执行顺序
2015-01-05 10:12
351 查看
在iphone移动开发中,经常用到TableView,总结了一下关于UITableViewDataSource 和 UITableViewDelegate的一些属性和方法。
@property(nonatomic,readonly) UITableViewStyle style //表格样式@property(nonatomic,assign) id <UITableViewDataSource> dataSource //数据源@property(nonatomic,assign) id <UITableViewDelegate> delegate //代理@property(nonatomic,getter=isEditing) BOOL editing //是否为编辑模式@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle //设置分隔线的样式@property(nonatomic,retain) UIColor *separatorColor //设置分隔线的颜色@property(nonatomic,retain) UIView *tableHeaderView //表头显示的视图@property(nonatomic,retain) UIView *tableFooterView //表尾显示的视图@property(nonatomic) BOOL allowsSelection //是否允许选中行@property(nonatomic) BOOL allowsSelectionDuringEditing //是否允许在编辑模式下选中行@property(nonatomic) BOOL allowsMultipleSelection //是否允许选中多行@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing //是否允许在编辑模式下选中多行- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style /
4000
/初始化一个UITableView,并且设置表格样式- (void)reloadData //重新访问数据源,刷新界面
#pragma mark - - - - - 执行顺序:(分区 - - 行 - - 编辑)
分区:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;一共有多少个分区- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;第section分区的头部标题- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;第section分区的底部标题- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section第section分区头部的高度- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section第section分区尾部的高度- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section第section分区头部显示的视图- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
第section分区尾部显示的视图行:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; (@required)第section分区一共有多少行- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;某一行的高度- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;(@required)创建第section分区第row行的UITableViewCell对象(indexPath包含了section和row),这个方法是必需的,他是产生UITableView内容的必须载体
编辑:- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以编辑(删除)- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;某一行是否可以移动来进行重新排序- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;UITableView右边的索引栏的内容- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath选中了UITableView的某一行在这个方法里你可以根据NSIndexPath判断相应的元素,然后做处理
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath设置每一行的等级缩进(数字越小,等级越高)- (void)setEditing:(BOOL)editing animated:(BOOL)animated是否要开启编辑模式<
f1a1
/p>- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated取消选中某一行,让被选中行的高亮颜色消失(带动画效果)- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier通过identifier在(缓存)池中找到对应的UITableViewCell对象- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation移除indexPaths范围内的所有行
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//行将显示的时候调用,预加载行-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *
//去掉cell多余的分割线
+ (void)setExtraCellLineHidden: (UITableView *)tableView
{
UIView *view = [UIViewnew];
view.backgroundColor = [UIColorclearColor];
[tableView setTableFooterView:view];
} self.listTableView.separatorColor = [UIColorclearColor];//分割线的颜色 self.listTableView.backgroundColor = [UIColorclearColor];//背景色 self.listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//想删除cell之间的分割线 self.listTableView.backgroundView = nil;//背景图片#pragma mark - ---- ScrollView扩展响应Touch事件//UISrollView的事件经常与其子view事件冲突,截断子view事件的相应,此方法可以传递touch事件@implementation UIScrollView (UITouchEvent)- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[selfnextResponder]touchesBegan:toucheswithEvent:event]; [supertouchesBegan:toucheswithEvent:event];}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [[selfnextResponder]touchesMoved:toucheswithEvent:event]; [supertouchesMoved:toucheswithEvent:event];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [[selfnextResponder]touchesEnded:toucheswithEvent:event]; [supertouchesEnded:toucheswithEvent:event];}
@end
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)i
@property(nonatomic,readonly) UITableViewStyle style //表格样式@property(nonatomic,assign) id <UITableViewDataSource> dataSource //数据源@property(nonatomic,assign) id <UITableViewDelegate> delegate //代理@property(nonatomic,getter=isEditing) BOOL editing //是否为编辑模式@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle //设置分隔线的样式@property(nonatomic,retain) UIColor *separatorColor //设置分隔线的颜色@property(nonatomic,retain) UIView *tableHeaderView //表头显示的视图@property(nonatomic,retain) UIView *tableFooterView //表尾显示的视图@property(nonatomic) BOOL allowsSelection //是否允许选中行@property(nonatomic) BOOL allowsSelectionDuringEditing //是否允许在编辑模式下选中行@property(nonatomic) BOOL allowsMultipleSelection //是否允许选中多行@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing //是否允许在编辑模式下选中多行- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style /
4000
/初始化一个UITableView,并且设置表格样式- (void)reloadData //重新访问数据源,刷新界面
#pragma mark - - - - - 执行顺序:(分区 - - 行 - - 编辑)
分区:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;一共有多少个分区- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;第section分区的头部标题- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;第section分区的底部标题- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section第section分区头部的高度- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section第section分区尾部的高度- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section第section分区头部显示的视图- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
第section分区尾部显示的视图行:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; (@required)第section分区一共有多少行- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;某一行的高度- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;(@required)创建第section分区第row行的UITableViewCell对象(indexPath包含了section和row),这个方法是必需的,他是产生UITableView内容的必须载体
编辑:- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以编辑(删除)- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;某一行是否可以移动来进行重新排序- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;UITableView右边的索引栏的内容- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath选中了UITableView的某一行在这个方法里你可以根据NSIndexPath判断相应的元素,然后做处理
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath设置每一行的等级缩进(数字越小,等级越高)- (void)setEditing:(BOOL)editing animated:(BOOL)animated是否要开启编辑模式<
f1a1
/p>- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated取消选中某一行,让被选中行的高亮颜色消失(带动画效果)- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier通过identifier在(缓存)池中找到对应的UITableViewCell对象- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation移除indexPaths范围内的所有行
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//行将显示的时候调用,预加载行-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *
//cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法编辑模式实现UITableViewDataSource的如下方法:- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 如果UITableView提交的是删除指令 if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除真实数据 // [self.data removeObjectAtIndex:indexPath.row]; // 删除UITableView中的某一行(带动画效果) [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 如果不考虑动画效果,也可以直接[tableView reload]; }}移动UITableView的行首先要开启编辑模式实现UITableViewDataSource的如下方法(如果没有实现此方法,将无法换行)- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { int from = sourceIndexPath.row; int to = destinationIndexPath.row; if (from == to) return; // 交换数据 // [self.data exchangeObjectAtIndex:from withObjectAtIndex:to];}
//去掉cell多余的分割线
+ (void)setExtraCellLineHidden: (UITableView *)tableView
{
UIView *view = [UIViewnew];
view.backgroundColor = [UIColorclearColor];
[tableView setTableFooterView:view];
} self.listTableView.separatorColor = [UIColorclearColor];//分割线的颜色 self.listTableView.backgroundColor = [UIColorclearColor];//背景色 self.listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//想删除cell之间的分割线 self.listTableView.backgroundView = nil;//背景图片#pragma mark - ---- ScrollView扩展响应Touch事件//UISrollView的事件经常与其子view事件冲突,截断子view事件的相应,此方法可以传递touch事件@implementation UIScrollView (UITouchEvent)- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[selfnextResponder]touchesBegan:toucheswithEvent:event]; [supertouchesBegan:toucheswithEvent:event];}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [[selfnextResponder]touchesMoved:toucheswithEvent:event]; [supertouchesMoved:toucheswithEvent:event];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [[selfnextResponder]touchesEnded:toucheswithEvent:event]; [supertouchesEnded:toucheswithEvent:event];}
@end
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)i
相关文章推荐
- viewController 和 tableView 相关方法执行顺序及作用
- UItableview全部属性、方法以及代理方法执行顺序
- UITableView的全部属性、方法以及代理方法执行顺序,看过之后肯定有收获---董鑫
- tableView代理方法执行顺序
- ios中tableview中的方法的执行顺序
- Checklists学习日志之UITableView的全部属性、方法以及代理方法执行顺序
- 类在构造时,构造方法内,类属性,父类属性,super的执行顺序
- 静态块,静态属性,成员变量,静态方法,构造函数执行顺序
- UITableView的全部属性、方法以及代理方法执行顺序
- -tableView: cellForRowAtIndexPath:方法不执行问题
- IOS中 ViewController中默认方法的执行顺序
- Java static属性和静态代码块和构造方法等的执行顺序探讨
- view的各个回调方法执行顺序
- java静态代码块/静态属性、构造块、构造方法执行、main方法、普通代码块的顺序
- UITableView方法的执行顺序
- android学习12——重载SurfaceView一些方法的执行顺序
- tableView 属性已经其协议方法
- 关于静态块、静态属性、构造块、构造方法的执行顺序
- iOS tableview 没有执行cellForRowAtInceldexPath 方法
- 为什么TableView 的代理方法cellForRowAtIndexPath 不执行问题