UITableView 详解

1.    UITableView的初始化

[csharp] view plaincopy
  1. UITableView tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  
  2.  [tableview setDelegate:self];  
  3.  [tableview setDataSource:self];  
  4.  [self.view addSubview: tableview];  
  5.  [tableview release];  



(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*)tableViewnumberOfRowsInSection:(NSInteger)section;

这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath;

这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。

3.    UITableView的委托方法

使用委托是为了响应用户的交互动作,比如下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供我们选择。

(1) 委托方法讲解

[csharp] view plaincopy
  1. //设置Section的数量  
  2. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  3.  return TitleData;  
  4. }  
  5. //设置每个section显示的Title  
  6. - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{  
  7.  return @"Andy-清风";  
  8. }  
  9.   
  10. //指定有多少个分区(Section),默认为1  
  11. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  12.  return 2;  
  13. }  
  14.   
  15. //指定每个分区中有多少行,默认为1  
  16. - (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{  
  17. }  
  18.   
  19. //设置每行调用的cell  
  20. -(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  21. static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
  22.     
  23.     UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:  
  24.                              SimpleTableIdentifier];  
  25.     if (cell == nil) {    
  26.         cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault  
  27.                                        reuseIdentifier:SimpleTableIdentifier] autorelease];  
  28.  }  
  29.  cell.imageView.image=image;//未选cell时的图片  
  30.  cell.imageView.highlightedImage=highlightImage;//选中cell后的图片  
  31.  cell.text=@”Andy-清风”;  
  32.  return cell;  
  33. }  
  34. //设置让UITableView行缩进  
  35. -(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{  
  36.  NSUInteger row = [indexPath row];  
  37.  return row;  
  38. }  
  39. //设置cell每行间隔的高度  
  40. - (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  41.     return 40;  
  42. }  
  43. //返回当前所选cell  
  44. NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];  
  45. [TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone];  
  46.   
  47. //设置UITableView的style  
  48. [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];  
  49. //设置选中Cell的响应事件  
  50. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{  
  51.   
  52.  [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失  
  53. }  
  54.   
  55. //设置选中的行所执行的动作  
  56.   
  57. -(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  58. {  
  59.     NSUInteger row = [indexPath row];  
  60.      return indexPath;  
  61. }  
  62. //设置划动cell是否出现del按钮,可供删除数据里进行处理  
  63. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {  
  64. }  
  65. //设置删除时编辑状态  
  66. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
  67. forRowAtIndexPath:(NSIndexPath *)indexPath  
  68. {  
  69. }  
  70.   
  71. //右侧添加一个索引表  
  72. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  73. }  



(2)  其他

[csharp] view plaincopy
  1. //选中cell时的颜色,在官方文档有如下可以选择  
  2.   
  3. typedef enum {  
  4.     UITableViewCellSelectionStyleNone,  
  5.     UITableViewCellSelectionStyleBlue,  
  6.     UITableViewCellSelectionStyleGray  
  7. } UITableViewCellSelectionStyle  
  8.   
  9.    
  10.   
  11. //cell右边按钮格式  
  12.   
  13. typedef enum {  
  14.     UITableViewCellAccessoryNone,                   //don't show any accessory view  
  15.     UITableViewCellAccessoryDisclosureIndicator,    //regular chevron. doesn't track  
  16.     UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks  
  17.     UITableViewCellAccessoryCheckmark               //checkmark. doesn't track  
  18. } UITableViewCellAccessoryType  
  19.   
  20.    
  21.   
  22. //是否加换行线  
  23.   
  24. typedef enum {  
  25.     UITableViewCellSeparatorStyleNone,  
  26.     UITableViewCellSeparatorStyleSingleLine  
  27. } UITableViewCellSeparatorStyle  
  28.   
  29.    
  30.   
  31. //改变换行线颜色  
  32.   
  33. tableView.separatorColor= [UIColor blueColor];  



 

4.    UITableViewCell

表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。


UITableViewCell为每个Cell提供了三个可以选择的属性,如下:

  • l textLabel:填写文本
  • l detailTextLable:稍微详细的副标题
  • l imageView:用来显示你cell的图片,可以通过UIImage来加载。

最后给出一个官方的demo给大家学习下,多实践,不懂的就问下,下节课讲些UITableView应用中实际会出现的问题,比如自定义啊,重用单元格,单元格的数据排序等问题。欢迎大家拍砖。

附上代码:http://download.csdn.net/detail/qiaoshe/3860668

本文转载至: http://blog.csdn.net/qiaoshe/article/details/7026056

//.......

//返回Section标题内容

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    }

    //自定义划动时del按钮内容

    - (NSString *)tableView:(UITableView *)tableView

    titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

    //跳到指的row or section

    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

    三、在UITableViewCell上建立UILable多行显示

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];

    [Datalabel setTag:100];

    Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [cell.contentView addSubview:Datalabel];

    [Datalabel release];

    }

    UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];

    [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];

    Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

    }

    //选中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

    //改变换行线颜色lyttzx.com

    tableView.separatorColor = [UIColor blueColor];

原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3139948.html