ios--UITableview的基本使用方法

来源:互联网 发布:c 和js交互 cef3 编辑:程序博客网 时间:2024/05/16 10:35

1.ViewController需要实现两个delegate(UITableViewDelegate/UITableViewDataSource),在.h文件中继承它们;并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承 UITableViewController实现的方法。


2.实现UITableView的数据源方法

UITableView是依赖外部资源为新表格单元填上内容的,我们成为数据源,这个数据源可以根据索引路径提供表格单元格,在UITableView中,索引路径是NSIndexPath的对象,可以选择分段或者分行,即是我们编码中的section和row.


UITableView有三个必须实现的核心方法:

(1)设置并返回tableview有多少部分(section)组成----分段显示(左图)或者单个列表显示数据(右图)

-(NSInteger)numberOfSectionsInTableView:(UITableView *) tableview{return 1;//返回有多少个section}

2)设置并返回每个分段的行数,不同的分段返回不同的行数可以用switch,如果是单个列表可以直接返回单个你想要的函数

-(NSInteger)tableView:(UITableView *)tableView  numberOfRowInSection:(NSInteger *)indexPath{   return 10;//返回对应section(indexPath)有多少元素(行数)}

(3)设置并返回每一个单元格(指定的 row的cell),一般在这定制各种个性化的 cell元素,其中有一个主标题cell.textLable,一个副标题cell.detailTextLable,还有一个 image在最前头cell.imageView,还可以设置右边的图标通过cell.accessoryType可以设置是饱满的向右的蓝色箭头还是单薄的向右箭头还是勾勾标记.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{   static NSString *cellIdentifier = @"showUserInfoCell";   UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:celldentifier] autorelease];   if(cell == nil){      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:             cellIdentifier] autorelease];   }   //configure the cell   cell.textLable.text = @"";   cell.detailTextLable.text = @"";

UITableView 的委托方法

(1)设置并返回行的间距( 高度)

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  return 70.0f;}
设置并返回指定的 section的 header view的高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  if(section == 0)     return 50.0f;  else     return 30.0f;}
设置并返回指定 section 的footer view的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{  return 20.0f;}

(2)返回指定 section的header的title,如果这个section header有返回view,那么title设置无效

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:section{    if(tableView == _tableView){         if(section == 0)            return @"";          else if             return @"";          else             return nil;    }else{        return nil;    }}

(3)返回指定section header的 view,如果没有,这个函数可以不返回 view

-(UIView)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{   if(section ==0){       UIView *header = [[[NSBundle mainBundle] loadNibNamed:@"SettingHeaderView" owner:self options:nil] lastObject];   }else{       return nil;   }}

(4)选中某行的 cell时的回调。首先必须设置tableView的allowsSelection属性为YES

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{   //选中某行时的操作   //例如view的转换(切换到user的页面)   [self.navigationController  pushViewController:user  animated:YES]}
(5)点击 cell右边箭头的回调(如果有箭头的话)

-(void)tableView:(UITableView *)tableView  accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{   //相应的操作}
(6)设置tableView可被编辑

 -(BOOL)tableView:(UITableView *)tableView  canEditRowAtIndexPath:(NSIndexPath *)indexPath{    TimeLineTableCell *cell= (TimeLineTableCell *)[self.tableView cellForRowAtIndexPath: indexPath];    //编辑状态下cell的某些操作,显示等    return YES;//可编辑,退出编辑设置为 NO}
(7)设置并返回当前cell要执行的是哪种编辑

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewCellEditingStyleDelete;//返回删除状态}
(8)通知用户编辑了那个 cell,并执行相应的操作

-(void)tableView:(UITableView *)tableView  commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:             (NSIndexPath *)indexPath{    if(editingStyle == UITableViewCellEditingStyleDelete){           //执行删除操作    }}
(9)获得某一行的cell对象

-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexpath;













原创粉丝点击