【UIKit】UITableView.02
生活随笔
收集整理的這篇文章主要介紹了
【UIKit】UITableView.02
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: citrix协议ICA技术原理
- 下一篇: Inna and Sequence