從storyboard中加載cell
1.往storyboard中拖入相關(guān)的控件 如圖
屏幕快照 2017-01-05 下午12.10.25.png
tableView控件右鍵delegate和dataSource與控制器關(guān)聯(lián)
2新建一個(gè)類TableViewCell繼承UITableViewCell,定義兩個(gè)屬性與storyboard中兩個(gè)控件相對(duì)應(yīng) 如圖 此時(shí)還是未關(guān)聯(lián)狀態(tài)
屏幕快照 2017-01-05 下午12.15.08.png
3.回到storyboard,點(diǎn)擊Table View Cell設(shè)置identifier:a , class設(shè)置為TableViewCell。
右鍵Table View Cell 將里面的img和label(在TableViewCell中定義的兩個(gè)屬性)與控件關(guān)聯(lián)起來(lái) 如圖
屏幕快照 2017-01-05 下午12.22.14.png
4.相關(guān)代碼
導(dǎo)入TableViewCell類,添加協(xié)議,定義一個(gè)數(shù)組
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSArray *array;
@end
解析plist文件的關(guān)鍵步驟就在這里了
-(NSArray *)array {
if (!_array) {
//創(chuàng)建可變數(shù)組用來(lái)接收遍歷的字典數(shù)據(jù)
NSMutableArray *mutableArr = [NSMutableArray array];
//讀取plist到arr中
NSString *path = [[NSBundle mainBundle] pathForResource:@"abc.plist" ofType:nil];
NSArray *arr = [NSArray arrayWithContentsOfFile:path];
//遍歷數(shù)組arr
for (NSDictionary *dict in arr) {
//字典添加到可變數(shù)組
[mutableArr addObject:dict];
}
//賦值給成員變量數(shù)組
_array = mutableArr;
}
//返回?cái)?shù)組
return _array;
}
自定義cell
//設(shè)置組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//設(shè)置行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.array.count;
}
//設(shè)置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//用TableViewCell這個(gè)類創(chuàng)建一個(gè)cell,Identifier和剛剛在storyboard中設(shè)置的一樣
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
if (!cell) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"a"];
}
//自定義cell里面的幾個(gè)控件內(nèi)容,self.array[indexPath.row]是一個(gè)字典
cell.label.text = [self.array[indexPath.row] objectForKey:@"text"];
cell.img.image = [UIImage imageNamed:[self.array[indexPath.row] objectForKey:@"icon"]];
return cell;
}
plist內(nèi)容
屏幕快照 2017-01-05 下午12.39.04.png
最終效果圖
b.gif