年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2509|回复: 0

[实用控件] ios实战开发之UITableView

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-31 22:59:02 | 显示全部楼层 |阅读模式
    1、效果演示



    2、过程分析

    2.1 创建一个UITableView
    2.2 设置UITableView的数据源为当前控制器
    2.3 当前控制器遵守UITableViewDataSource协议并实现以下几个方法
    1. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    2. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    3. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    4. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    5. -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    复制代码
    3、关键代码
    SAViewController.m
    1. //
    2. //  SAViewController.m
    3. //  UITableView
    4. //
    5. //  Created by yusian on 14-3-31.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SAViewController.h"
    9. // 定义几个宏
    10. #define kFile   @"address.plist"
    11. #define kUrl    @"http://yusian.com/download/data/address.plist"
    12. #define kHeader @"province"
    13. #define kCities @"cities"
    14. #define kFooter @"description"
    15. @interface SAViewController () <UITableViewDataSource>
    16. {
    17.     NSArray *_addressBook;
    18. }
    19. @end
    20. @implementation SAViewController
    21. - (void)viewDidLoad
    22. {
    23.     [super viewDidLoad];
    24.    
    25.     // 创建一个UITableView初始化为组模式,并将当前控制器设置为数据源(代理)
    26.     UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    27.     [self.view addSubview:tableView];
    28.     tableView.dataSource = self;
    29.    
    30.     // 准备本地数据,从本地文件读取数据到数组(该数组为多维数据,plist文件事按照相关格式存储数据
    31.     // plist文件格式为NSArray[NSDictionary[NSString、NSArray、NSString]、NSDictionary[NSString、NSArray、NSString]...]
    32.     // 解释:数组基本元素为字典(一个省份),每个字典里面包含三个对象,两个字符串(该省名称和描述信息)和一个数组(该省份的城市);
    33.     NSString *path = [[NSBundle mainBundle] pathForResource:kFile ofType:nil];
    34.     _addressBook = [[NSArray alloc] initWithContentsOfFile:path];   // 加载本地文件到数组_addressBook
    35.    
    36.     // 准备互联网数据,通过url从互联网下载plist文件;
    37.     // NSURL *url = [NSURL URLWithString:kUrl];
    38.     // _addressBook = [[NSArray alloc] initWithContentsOfURL:url];
    39. }
    40. // UITableViewDataSource协议中的方法,返回TabelView组数,默认无实现则为1,数组长度即省份数,即分组数
    41. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    42. {
    43.     return _addressBook.count;
    44. }
    45. // UITableViewDataSource协议中必须实现的两个方法之一,返回每组的行数,这里即各省份的城市数,所以取各字典中子数组的长度
    46. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    47. {
    48.     return [_addressBook[section][kCities] count];
    49. }
    50. // UITableViewDataSource协议中必须实现的两个方法之一,给UITable中cell元素赋值,这里以cell的textLable.text为例
    51. // 通过textLabel的text属性,将每个城市显示在UITable中
    52. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    53. {
    54.     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    55.     cell.textLabel.text = _addressBook[indexPath.section][kCities][indexPath.row];
    56.     return cell;
    57. }
    58. // 定义每个组的Header,即省份名称
    59. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    60. {
    61.     return _addressBook[section][kHeader];
    62. }
    63. // 定义每个组的Footer,即省份描述
    64. -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    65. {
    66.     return _addressBook[section][kFooter];
    67. }
    68. @end
    复制代码
    4、源代码下载
    游客,本帖隐藏的内容需要积分高于 1 才可浏览,您当前积分为 0
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-27 19:28 , Processed in 0.051686 second(s), 23 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表