• 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;

    }

  • 相关阅读:
    【LeetCode】二叉树相关题解汇总
    亚马逊电商网红营销实战技巧分享
    手写raft(三) 实现日志压缩
    php计算机毕业设计基于thinkphp框架的特色旅游网站vue
    百度Q3财报显AI技术厚度,“慢生意”稳步驶入“快车道”
    终于等来了这本用Rust进行系统编程的实践指南
    腾讯云服务器99元一年?假的,阿里云是99元
    【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
    (一)Spring启示录
    Activiz快速显示大量球体
  • 原文地址:https://blog.csdn.net/qq_34491373/article/details/126420372