• iOS开发基础-UITableView控件简单介绍


       UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动。

       UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么内容等。凡是遵守 UITableViewDataSource 协议的Objc对象,都可以是 UITableView 的数据源。

       - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  返回共有多少组数据。

       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  返回每一组有多少行数据。

       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  返回每一行显示的内容。

       - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section  返回各组底部显示的标题。

       - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  返回各组头部显示的标题。

    实例

      新建一个Single View Application,命名为TableViewDemo,首先让 ViewController 类遵守 UITableViewDataSource 协议,并声明一个 UITableView 属性,如下所示:

    1 //ViewController.m
    2 @interface ViewController () <UITableViewDataSource>
    3 @property (weak, nonatomic) IBOutlet UITableView *tableView;
    4 @end

      将Table View视图拖到storyboard中,并将其与 tableView 属性建立关联。

      接下来设置 tableView 的数据源,修改 viewDidLoad 方法:

    1 //ViewController.m
    2 - (void)viewDidLoad {
    3     [super viewDidLoad];
    4     self.tableView.dataSource = self;
    5 }

      重写 UITableViewDataSource 协议的方法:

     1 #pragma mark - UITableViewDataSource
     2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     3     NSLog(@"%s", __FUNCTION__);
     4     return 2;
     5 }
     6 
     7 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     8     NSLog(@"%s", __FUNCTION__);
     9     if (section == 0) {
    10         return 2;
    11     } else {
    12         return 3;
    13     }
    14 }
    15 
    16 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    17     NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row);
    18     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    19     if (indexPath.section == 0) {
    20         if (indexPath.row == 0) {
    21             cell.textLabel.text = @"奥迪";
    22         } else if (indexPath.row == 1) {
    23             cell.textLabel.text = @"宝马";
    24         }
    25     } else if (indexPath.section == 1) {
    26         if (indexPath.row == 0) {
    27             cell.textLabel.text = @"本田";
    28         } else if (indexPath.row == 1) {
    29             cell.textLabel.text = @"丰田";
    30         } else if (indexPath.row == 2) {
    31             cell.textLabel.text = @"马自达";
    32         }
    33     }
    34     return cell;
    35 }
    36 
    37 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    38     if (section == 0) {
    39         return @"高端大气上档次";
    40     } else {
    41         return @"还不错";
    42     }
    43 }
    44 
    45 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    46     if (section == 0) {
    47         return @"德系品牌";
    48     } else {
    49         return @"日韩品牌";
    50     }
    51 }

       UITableViewCell 有一个 UITableViewCellStyle 属性,用于觉得其 contentView 使用哪些子类型,该属性定义如下:

    1 typedef enum : NSInteger {
    2    UITableViewCellStyleDefault,
    3    UITableViewCellStyleValue1,
    4    UITableViewCellStyleValue2,
    5    UITableViewCellStyleSubtitle 
    6 } UITableViewCellStyle;

      为显示的内容按组建立数据模型类,命名为 WJQCarGroup ,声明下列属性:

    1 //WJQCarGroup.h
    2 @interface WJQCarGroup : NSObject
    3 @property (nonatomic, copy) NSString *title;
    4 @property (nonatomic, copy) NSString *desc;
    5 @property (nonatomic, strong) NSArray *cars;    //当前组所有行的数据
    6 @end

      接下来在 ViewController.m 文件中导入 WJQCarGroup.h ,并在类扩展中声明 NSArray 属性用来存放所有组的数据,如下:

    1 //ViewController.m
    2 @interface ViewController () <UITableViewDataSource>
    3 @property (weak, nonatomic) IBOutlet UITableView *tableView;
    4 @property (nonatomic, strong) NSArray *carGroups;   //保存所有组的数据
    5 @end

      使用懒加载技术重写 carGroups 属性的 getter 方法,如下:

     1 //ViewController.m
     2 #pragma mark - 懒加载
     3 - (NSArray *)carGroups {
     4     if (_carGroups == nil) {
     5         WJQCarGroup *cg1 = [[WJQCarGroup alloc] init];
     6         cg1.title = @"德系品牌";
     7         cg1.desc = @"高端大气上档次";
     8         cg1.cars = @[@"奥迪", @"宝马"];
     9         
    10         WJQCarGroup *cg2 = [[WJQCarGroup alloc] init];
    11         cg2.title = @"日韩系列";
    12         cg2.desc = @"还不错";
    13         cg2.cars = @[@"本田", @"丰田"];
    14         
    15         WJQCarGroup *cg3 = [[WJQCarGroup alloc] init];
    16         cg3.title = @"欧美品牌";
    17         cg3.desc = @"价格昂贵";
    18         cg3.cars = @[@"劳斯莱斯", @"布加迪", @"小米"];
    19         _carGroups = @[cg1, cg2, cg3];
    20     }
    21     return _carGroups;
    22 }

      最后,修改 UITableViewDataSource 协议的相关方法:

     1 //ViewController.m
     2 #pragma mark - UITableViewDataSource
     3 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     4     NSLog(@"%s", __FUNCTION__);
     5     return self.carGroups.count;
     6 }
     7 
     8 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     9     NSLog(@"%s", __FUNCTION__);
    10     WJQCarGroup *carGroup = self.carGroups[section];
    11     return carGroup.cars.count;
    12 }
    13 
    14 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    15     NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row);
    16     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    17     WJQCarGroup *carGroup = self.carGroups[indexPath.section];
    18     NSString *carName = carGroup.cars[indexPath.row];
    19     cell.textLabel.text = carName;
    20     return cell;
    21 }
    22 
    23 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    24     WJQCarGroup *carGroup = self.carGroups[section];
    25     return carGroup.desc;
    26 }
    27 
    28 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    29     WJQCarGroup *carGroup = self.carGroups[section];
    30     return carGroup.title;
    31 }

    参考博客:iOS开发UI篇—UITableview控件简单介绍

    实例代码:http://vdisk.weibo.com/s/DiY98QyXCOne0

     

    1 //ViewController.m
    2 #pragma mark - 控制状态栏是否显示
    3 - (BOOL)prefersStatusBarHidden {
    4     //返回YES代表隐藏状态栏,NO相反
    5     return YES;
    6 }
  • 相关阅读:
    解决PyQt5在安装后无法找到Designer.exe问题,两个位置可供参考
    观察者模式
    策略模式
    模板方法模式(下)
    学过的技术容易忘,怎么办?
    Mysql主从配置
    Springboot处理CORS跨域请求
    SpringBoot学习2之注解简单配置Springboot+MyBatis
    Confluence7.4安装并破解汉化教程
    mysql json类型
  • 原文地址:https://www.cnblogs.com/wjq-Law/p/5195466.html
Copyright © 2020-2023  润新知