您的位置:首页 > 产品设计 > UI/UE

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右边按钮格式为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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息