zoukankan      html  css  js  c++  java
  • UITableView基本属性应用

    一 创建表格,并把表格添加到控制器

        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];//创建表格   指定表格的宽高为self.view.bounds就是铺满全屏    设置表格样式为分组样式UITableViewStyleGrouped  还有一个样式为UITableViewStylePlain 普通样式

        tableView.dataSource = self;//设置表格显示数据的数据源,这个必须遵循UITableViewDataSource这个协议,如果不遵循这个协议会调用不到绑定数据的方法

        [self.view addSubview:tableView]; 把表格添加到控制器

    二 设置表格绑定数据和每行cell绑定的方法

    #pragma mark - 数据源方法

    #pragma mark 一共有多少组(section == 区域组)

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView    //numberOfSenctionsInTableView方法为反回数据有多少组,并把这个组传给反回行方法,这个是自动调用的

    {

    //    NSLog(@"numberOfSections"); 

        return 3;  返回3组

    }

    #pragma mark 第section组一共有多少行

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //numberOfRowsInSection为返回多少行的方法

    {

         senction  // 这参数为上面方法传过来的第几组,这里接怍到第几组后,把相对应有几行返回过去

    }

    #pragma mark 返回每一行显示的内容(每一行显示怎样的cell)

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

    {

       //这里接收第几组第几行的索引(indexPath.Senction,indexPath.Row),然后定义UITableViewCell 通过设置cell的相应属性并把cell返回,这样就完成了每一行显示什么数据

        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

        cell.textLabel.text = text;

        return cell;

    }

    #pragma mark 第section组显示的头部标题

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

    {

            return @"表头";

    }

     

    #pragma mark 第section组显示的尾部标题

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

    {

        return @"结尾";

    }

    #pragma mark 返回表格右边的显示的索引条

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView   //该方法为显示索引条,物别注意动态数组的添加方式

    {

        NSMutableArray *titles = [NSMutableArray array];

        

        for (Province *p in _allProvinces) {

            [titles addObject:p.header];

        }

        

        return titles;

    }

  • 相关阅读:
    CentOS7 设置软件镜像源
    让树莓派自动上报IP地址到邮箱,二代B
    给树莓派安装看门狗的两种方法,二代B
    树莓派(Raspberry Pi)USB无线网卡自动连接,二代B
    升级MAC OX上的Python到3.4
    http 返回码 405 解决方案之一
    CentOS 6.4 SSH 免密码登录
    SVN的Hooks功能--强制添加注释
    PHP开发中,让var_dump调试函数输出更美观 ^_^#
    CentOS 6.4 命令行 安装 VMware Tools
  • 原文地址:https://www.cnblogs.com/xgj0721/p/4340294.html
Copyright © 2011-2022 走看看