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

    }

  • 相关阅读:
    canvasnode的设计思路和api介绍
    希望新浪网络学院的童鞋们加油
    和新童鞋们吃饭,见到了jeremy
    MongoDB 学习资料
    [转] Scrum框架及其背后的原则
    twisted学习资料
    创建课程表
    进程
    协程
    支付宝支付流程
  • 原文地址:https://www.cnblogs.com/xgj0721/p/4340294.html
Copyright © 2020-2023  润新知