• IOS 中列表的TableView 详解,常用方法整理


    一、创建一个列表,不管代码还是nib拖拉,在nib创建的时候,记得加他的二个代理 (UITableViewDelegate UITableViewDataSource)

    代码创建的话,需要关联他的代理,nib创建则不需要关联,他自动会关联。。。代码创建关联的方法为  

    1 LXDataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
    2 
    3 [LXDataTable setDelegate:self];
    4 
    5 [LXDataTable setDataSource:self];
    6 
    7 [self.view addSubview:DataTable];
    8 
    9 [LXDataTable release];

    二、UITableview的每一个代理的Method 使用和 讲解

      1 //Section总数
      2 
      3 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
      4 
      5  return TitleData;
      6 
      7 }
      8 
      9 // Section Titles
     10 
     11 //每个section显示的标题
     12 
     13 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
     14 
     15  return @"";
     16 
     17 }
     18 
     19 //指定有多少个分区(Section),默认为1
     20 
     21 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     22 
     23  return 4;
     24 
     25 }
     26 
     27 //指定每个分区中有多少行,默认为1
     28 
     29 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     30 
     31 }
     32 
     33 //绘制Cell
     34 
     35 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     36 
     37 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
     38 
     39     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
     40 
     41                              SimpleTableIdentifier];
     42 
     43     if (cell == nil) {  
     44 
     45         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
     46 
     47                                        reuseIdentifier: SimpleTableIdentifier] autorelease];
     48 
     49  }
     50 
     51  cell.imageView.image=image;//未选cell时的图片
     52 
     53  cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
     54 
     55  cell.text=//.....
     56 
     57  return cell;
     58 
     59 }
     60 
     61 //行缩进
     62 
     63 -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
     64 
     65  NSUInteger row = [indexPath row];
     66 
     67  return row;
     68 
     69 }
     70 
     71 //改变行的高度
     72 
     73 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     74 
     75     return 40;
     76 
     77 }
     78 
     79 //定位
     80 
     81 [TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
     82 
     83 //返回当前所选cell
     84 
     85 NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
     86 
     87 [TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
     88 
     89 [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
     90 
     91 //选中Cell响应事件
     92 
     93 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     94 
     95  [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
     96 
     97 }
     98 
     99 //判断选中的行(阻止选中第一行)
    100 
    101 -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    102 
    103 {
    104 
    105     NSUInteger row = [indexPath row];
    106 
    107     if (row == 0)
    108 
    109         return nil;
    110 
    111     return indexPath;
    112 
    113 }
    114 
    115 //划动cell是否出现del按钮
    116 
    117 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    118 
    119 }
    120 
    121 //编辑状态
    122 
    123 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
    124 
    125 forRowAtIndexPath:(NSIndexPath *)indexPath
    126 
    127 {
    128 
    129 130 
    131 [topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
    132 
    133 //右侧添加一个索引表
    134 
    135 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    136 
    137 }
    138 
    139 //返回Section标题内容
    140 
    141 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    142 
    143 }
    144 
    145 //自定义划动时del按钮内容
    146 
    147 - (NSString *)tableView:(UITableView *)tableView
    148 
    149 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    150 
    151 //跳到指的row or section
    152 
    153 [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

    三、在UITableViewCell上建立多个 UILable ,来代替cell的加载显示

     1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     2 
     3     static NSString *CellIdentifier = @"Cell";   
     4 
     5     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     6 
     7     if (cell == nil) {
     8 
     9         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    10 
    11   UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
    12 
    13   [Datalabel setTag:100];
    14 
    15   Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    16 
    17   [cell.contentView addSubview:Datalabel];
    18 
    19   [Datalabel release];
    20 
    21  } 
    22 
    23  UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
    24 
    25  [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
    26 
    27  Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
    28 
    29  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    30 
    31     return cell;
    32 
    33 }
    34 
    35 //选中cell时的颜色
    36 
    37 typedef enum {
    38 
    39     UITableViewCellSelectionStyleNone,
    40 
    41     UITableViewCellSelectionStyleBlue,
    42 
    43     UITableViewCellSelectionStyleGray
    44 
    45 } UITableViewCellSelectionStyle 
    46 
    47 //cell右边按钮格式
    48 
    49 typedef enum {
    50 
    51     UITableViewCellAccessoryNone,                   // don't show any accessory view
    52 
    53     UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
    54 
    55     UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
    56 
    57     UITableViewCellAccessoryCheckmark               // checkmark. doesn't track
    58 
    59 } UITableViewCellAccessoryType
    60 
    61 //是否加换行线
    62 
    63 typedef enum {
    64 
    65     UITableViewCellSeparatorStyleNone,
    66 
    67     UITableViewCellSeparatorStyleSingleLine
    68 
    69 } UITableViewCellSeparatorStyle//改变换行线颜色
    70 
    71 tableView.separatorColor = [UIColor blueColor];
  • 相关阅读:
    ajax设置自定义请求头
    tomcat修改端口号
    下载文件(前端+后台)
    ajax请求超时判断(转载)
    js判断变量类型
    js中判断是否包含某个字符串
    Java将json字符串转成map
    Linux上安装设置mysql 5.7.24
    js格式化json字符串和json对象
    bootstrap中的data-toggle模态框相关
  • 原文地址:https://www.cnblogs.com/Wild-orangutans/p/4466206.html
Copyright © 2020-2023  润新知