UITableView.02:

【1】拖入一个UITableView

【2】将TableView的dataSource与控制器连接

【3】首先得遵循UITableView的数据源协议<UITableViewDataSource>

【4】将数据plist文件拖入

    

【5】代码

1.viewDidLoad只加载一次,所以用来加载plist文件中的数据。

  新建一个bundel用来指定文件路径

  pathForResource:文件名
  ofType:文件格式
- (void)viewDidLoad
{[super viewDidLoad];NSBundle *bundle=[NSBundle mainBundle]; // 获取路径的方法// 加载数据self.provinces=[NSArray arrayWithContentsOfFile:[bundle pathForResource:@"provinces" ofType:@"plist"]];// 获取路径需要上面的bundleself.cities=[NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:@"cities" ofType:@"plist"]];// 获取路径需要上面的bundle
}

2.设置组数,通过对provinces.plist文件中省份的count,来返回一共有多少组

#pragma mark -数据源方法
#pragma mark 一共多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{// 返回省份的个数,也就是多少组return self.provinces.count;
}

3.设置第section组有多少行(先得到第i个组,通过这个组到数组中去调取有多少行,返回对应城市行数)

  首先要设置property

@interface ViewController ()
// 省份
@property(nonatomic,strong)NSArray *provinces;
// 城市
@property (nonatomic,strong)NSDictionary *cities;@end

#pragma mark 第section组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{// 返回省对应城市的成员个数// 取出第section组的省份名称NSString *pName=self.provinces[section];// 取出对应城市NSArray *myCities=self.cities[pName];// 返回对应城市成员个数return myCities.count;}

4.用来返回某一行对应的数据

#pragma mark 返回某一行对应的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   // 创建一个cell// UITableViewCellStyleDefault 是默认的style// reuseIdentifier 标识符默认为nilUITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];// 1.首先取出第section组的省份名称NSString *pName= self.provinces[indexPath.section];// 2.根据上面取出的省份名称来取出这个省份的所有城市NSArray *myCities=self.cities[pName];// 取出对应的城市名称cell.textLabel.text=myCities[indexPath.row];return cell;
}

5.返回头部标题

#pragma mark 返回头部标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{return self.provinces[section];
}

6.返回索引【此处是使用省份作为索引,如果用拼音首字母作为索引的话,需要调取第三方内容】

#pragma mark -添加索引条,标题索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{//return @[@"A",@"B",@"C"];// 返回所有省份return self.provinces;
}

转载于:https://www.cnblogs.com/iflewless/p/3891206.html

【UIKit】UITableView.02相关推荐

  1. 【UIKit】UITableView 自定义Cell2

    [自定义Cell2][Code]:全部通过代码添加 [1]:设置Cell 1):创建一个MsgCell类 继承UITableViewCell .h中声明2个属性一个是用户头像,另外一个是发表的文字 ...

  2. 【UIKit】UITableView 1

    UITableView:代码 section:组别 row:行号 [1]拖入一个UITableView [2]将TableView的dataSource与控制器连接 [3]首先得遵循UITableV ...

  3. 【UIKit】UITableView 3

    UITableView 代码 [1]拖入一个UITableView [2]将TableView的dataSource与控制器连接 [3]首先得遵循UITableView的数据源协议<UITab ...

  4. 【UIKit】UITableView.06

    UITableView.06: [1]拖入ToolBar,TableView [2]连线,设置代理模式,数据源等(ToolBar中的垃圾桶也需要连接方法removeRow) [3]代码 1.声明 my ...

  5. 【UIKit】表格 UITableView

    UITableView的使用介绍 官方文档:Table View Programming Guide for iOS UITableView基本使用基本步骤 准备数据源的数组 遵循的协议UITable ...

  6. 【UIKit】表格自定义单元格(UITableViewCll)

    自定义表格单元格(Cell)操作 使用NIb文件自定义单元格(Cell) 1.          创建UITableViewCell的子类,创建的同时记得选择生成xib文件(当然你也可以将控件通过代码 ...

  7. 【UIKit】UIView基础学习

    UIView基础介绍 官网文档: View programming Guide for iOS UIView Class Reference [常用属性和方法] UIKit User Interfac ...

  8. 【UIKit】文本框(UITextField)及键盘遮挡处理和回收

    文本框(UITextField)及键盘遮挡处理和回收 文本框属性 官网文档:UITextField Class Reference 以下都是可以在IB中选择设置的,也可以使用代码编写 placehol ...

  9. 【UIKit】TabView

    [TabView] [下面的tab栏][code] [第一步]:创建一个Tabbed Application [第二步]:加入图片素材. [第三步]:创建3个有xib的类文件 FirstViewCo ...

最新文章

  1. 《松本行弘的程序世界》中文版原作者序
  2. 女程序猿2014总结(修改)
  3. 天地图 热力图_arcgis js 3.x使用webgl绘制热力图
  4. java微妙_10个微妙的Java编码最佳实践
  5. UE3 ExampleGame Android版无法运行解决方案
  6. m.2接口和nvme区别_原来M.2、SATA接口的固态盘有这样的差别,难怪大家都选“它”!...
  7. poj 2371 Questions and answers
  8. 开源操作系统年度技术会议图文直播
  9. matlab负反馈传函,已知负反馈系统开环传函求阶跃传函
  10. 联发科四核处理器MT6589详细芯片参数介绍
  11. Seven languages in seven weeks (notes on Scala)
  12. 【jzoj1747】马蹄印
  13. matlab矩阵的白化,白化原理及Matlab实现
  14. IMAP4协议介绍nbsp;工作原理
  15. Mac 外接显示器色彩不正常解决方案
  16. Python学习笔记 | 编码和文件读写
  17. vue前端上传doc文件并读取文件内容
  18. DolphinDB Database丨 最简最快的WorldQuant 101 Alpha因子实现
  19. 1325208-25-0,Mal-PEG4-NHS马来酰亚胺基团和NHS酯基团交联剂
  20. golang websocket 一个语音聊天室

热门文章

  1. windows上使用image库
  2. Android URLconnection
  3. Spring Web Flow 2.0 入门详解
  4. AccountManagment
  5. shell 数学运算
  6. 2020-11-24
  7. CentOS7手动修改系统时间
  8. 运行startup.bat的启动过程
  9. [Swift]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted
  10. TensorFlow 从零到helloWorld