zoukankan      html  css  js  c++  java
  • (4) IOS笔记本——UITableView的基本应用

    ◇UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳,在特定情况下我们使用UIScrollView而不是UITableView。

    ◇UITableView通过一个数据源(dataSource)来显示数据,会向数据源查询一共有多少行数据,以显示什么数据。

    ◇举一个小例子,我们要做如下图的的一个下拉表格:

                                                           

    ◇步骤为:

        ◇1、利用MVC的设计模式建立模型,(MVC的思想:只要修改了模型,视图界面一并跟着修改)

        ◇2、准守协议,设置模型数据

        ◇3、设置UITableView的各个属性,并且绑定数据源

    ◇具体实现方法:

        ◇1、建立模型,新建一个cocoa文件,把下面代码写在.h文件的定义(@interface)中

    1 //用于描述标题
    2 @property (nonatomic,copy) NSString *title;
    3 
    4 //用于描述详细描述
    5 @property (nonatomic,copy) NSString *desc;
    6 
    7 //用于存放汽车品牌
    8 @property (nonatomic,strong) NSArray *car;

        ◇2、准守协议,设置模型数据

     1 @interface ViewController () <UITableViewDataSource>  //准守协议
     2 。。。 。。。 。。。
     3 @property (nonatomic,strong) NSArray *carGroups;//定义数组变量
     4 。。。 。。。 。。。
     5 //设置模型数组
     6 -(NSArray *)carGroups{
     7     if(_carGroups == nil){
     8         
     9         //德系品牌
    10         CarGroup *cg1 = [[CarGroup alloc] init];
    11         cg1.title = @"德系品牌";
    12         cg1.desc = @"德国品牌德国品牌德国品牌德国品牌";
    13         cg1.car = @[@"大众",@"奔驰",@"奥迪"];
    14         
    15         //日系品牌
    16         CarGroup *cg2 = [[CarGroup alloc] init];
    17         cg2.title = @"日系品牌";
    18         cg2.desc = @"日系品牌日系品牌日系品牌日系品牌";
    19         cg2.car = @[@"丰田",@"本田",@"因菲妮迪"];
    20         
    21         //欧系品牌
    22         CarGroup *cg3 = [[CarGroup alloc] init];
    23         cg3.title = @"欧系品牌";
    24         cg3.desc = @"欧系品牌欧系品牌欧系品牌欧系品牌";
    25         cg3.car = @[@"劳斯莱斯",@"兰博基尼",@"宾利",@"劳斯莱斯",@"兰博基尼",@"宾利"];
    26     
    27         _carGroups = @[cg1, cg2, cg3];
    28     }
    29     return _carGroups;
    30 }

        ◇3、设置UITableView的各个属性,并且绑定数据源

     1 //设置一共有多少组数据
     2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     3     return self.carGroups.count;
     4 }
     5 
     6 //设置每组数据有多少行信息
     7 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     8     CarGroup *cg = self.carGroups[section];
     9     return cg.car.count;
    10 }
    11 
    12 //设置每个cell的内容
    13 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    14     
    15     //实例化cell对象
    16     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    17     
    18     // 取出第indexPath.section组对应的模型
    19     CarGroup *cg = self.carGroups[indexPath.section];
    20     
    21     // 取车第indexPath.row这行对应的品牌名称
    22     NSString *car = cg.car[indexPath.row];
    23     
    24     // 设置cell显示的文字
    25     cell.textLabel.text = car;
    26     
    27     return cell;
    28 }

         ◇附一张UITableViewCell的UITableViewCellStyle枚举类型属性图:

                                                    

  • 相关阅读:
    hdu 4777 树状数组+合数分解
    hdu5635 BestCoder Round #74 (div.2)
    hdu 5636 搜索 BestCoder Round #74 (div.2)
    hdu 5637 BestCoder Round #74 (div.2)
    hdu4605 树状数组+离散化+dfs
    hdu4521 线段树+dp
    hdu3340 线段树+多边形
    孜孜不倦,必能求索;风尘仆仆,终有归途。
    增加、删除类文件或者在一个类中增加、删除方法时,是不能够热部署到服务上的。这时候需要停止服务器重新部署后再启动,就不会出现上面的提示了。
    为什么jdk1.8不支持sql.append,该如何解决
  • 原文地址:https://www.cnblogs.com/kaolalovemiaomiao/p/5257128.html
Copyright © 2011-2022 走看看