• iOS 关于UITableView常见使用方法


    Step—1:基本用法

    //声明对象

    @property(nonatomic,strong) UITableView*tableView;

    //遵守代理

    //创建

     /*创建表视图

         格式:UITableViewStylePlain 普通 UITableViewStyleGrouped 分组

     */

    self.tableView= [[UITableViewalloc] initWithFrame:self.view.framestyle:UITableViewStylePlain];

     self.tableView.delegate= self;

     self.tableView.dataSource= self;

     //属性设置 行高

     self.tableView.rowHeight= 45;

     //属性设置 节头视图高

     self.tableView.sectionHeaderHeight= 45;

     //属性设置 节脚视图高

     self.tableView.sectionFooterHeight= 45;

      [self.viewaddSubview:self.tableView];

     // UITableViewDataSource 方法 为表视图单元格提供数据,必须要实现。

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

     staticNSString*idetifierCell = @"cell";

     UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:idetifierCell];

     if(!cell) {

     /*

             UITableViewCellStyleDefault 默认模式,只有图标和主标题

             UITableViewCellStyleValue1  Value1样式 有主标题和副标题,主标题左对齐 副标题右对齐,可以有图标。

             UITableViewCellStyleValue2 Value2 有主标题和副标题,主标题和副标题 剧中对齐,无图标

             UITableViewCellStyleSubtitle Subtitle 样式 有图标,主标题和副标题。副标题在主标题下面

             */

            cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:idetifierCell];

        }

        cell.imageView.image= [UIImageimageNamed:@"icon"];

        cell.textLabel.text= @"艾米尼队";

        cell.detailTextLabel.text= @"为主打奥运而永垂不朽!";

        return cell;

    }

    //返回节数

    -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

     return 7;

    }

    //返回某个节中行数

    -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

     return 3;

    }

    //节头的标题

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

     return @"头标题";

    }

    //节脚的标题

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

     return @"脚标题";

    }

    //提供索引标题

    -(NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView{

     return@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"U",@"V",@"W",@"X",@"Y",@"Z"];

    }

    //为删除和修改提供数据

    - (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

    }

    //代理提供行高

    - (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

     return 45;

    }

    //代理 节头视图高

    - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{

     return 20;

    }

    //代理 节尾视图高

    - (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{

     return 40;

    }

    // UITableViewDelegate 方法 节头自定义视图

    -(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{

     UIView*headVew = [[UIViewalloc] init];

        headVew.backgroundColor= [UIColororangeColor];

     returnheadVew;

    }

    //节脚自定义视图

    -(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section{

     UIView*footerVew = [[UIViewalloc] init];

        footerVew.backgroundColor= [UIColorredColor];

     return footerVew;

    }

    //该方法在节头从屏幕中消失时候触发

    -(void)tableView:(UITableView*)tableView didEndDisplayingHeaderView:(nonnullUIView*)view forSection:(NSInteger)section{

     NSLog(@"节头消失了...");

    }

    //该方法在节脚从屏幕中消失时候触发

    -(void)tableView:(UITableView*)tableView didEndDisplayingFooterView:(UIView*)view forSection:(NSInteger)section{

     NSLog(@"节脚消失了...");

    }

    //该方法在cell单元格消失中触发

    -(void)tableView:(UITableView*)tableView didEndDisplayingCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath{

     NSLog(@"cell单元格消失了...");

    }

    //该方法在cell响应时触发

    -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

     NSLog(@"cell被点击了...");

    }

    //响应单元格水平滑动事件

    -(NSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath{

     return nil;

    }

  • 相关阅读:
    【DSP】【第二篇】了解C6678和创建工程
    Git链接上游仓库
    Linux文件编程详解
    大数据Flink(九十二):DML:集合操作
    Linux常用命令——cmp命令
    【代码岁想录】哈希表刷题
    【C++的OpenCV】第十四课-OpenCV基础强化(二):访问单通道Mat中的值
    多模态交互式 AI 代理的兴起:探索 Google 的 Astra 和 OpenAI 的 ChatGPT-4o应用
    Java题目汇总(二)
    【uni-app从入门到实战】组件学习
  • 原文地址:https://blog.csdn.net/qq_34491373/article/details/126420372