// ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//遵循协议
@property (strong,nonatomic) UITableView *gameTableView;
@property (strong,nonatomic) NSMutableArray *marrName;
@property (strong,nonatomic) NSMutableArray *marrIcon;
@property (strong,nonatomic) NSMutableArray *marrDownload;
@end
// ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//文件导入:导入一个plist文件
NSString *path=[[NSBundle mainBundle]pathForResource:@"apps" ofType:@"plist"];
NSArray *array=[NSArray arrayWithContentsOfFile:path];
//初始化三个可变集合
self.marrName=[[NSMutableArray alloc]initWithCapacity:0];
self.marrIcon=[[NSMutableArray alloc]initWithCapacity:0];
self.marrDownload=[[NSMutableArray alloc]initWithCapacity:0];
//循环便利plist文件的值存入三个可变集合里
for (int i=0; i<array.count; i++)
{
NSDictionary *dic=array[i];
[self.marrName addObject:[dic objectForKey:@"name"]];
[self.marrIcon addObject:[dic objectForKey:@"icon"]];
[self.marrDownload addObject:[dic objectForKey:@"download"]];
}
//初始化tableView
self.gameTableView =[[UITableView alloc]initWithFrame:self.view.frame style:0];
//tableview的分隔符颜色
self.gameTableView.backgroundColor=[UIColor lightGrayColor];
//添加指定代理
self.gameTableView.delegate=self;
self.gameTableView.dataSource=self;
//设置tableView的行高
self.gameTableView.rowHeight=60;
//给tableView添加背景色
self.gameTableView.separatorColor=[UIColor redColor];
//添加到父视图
[self.view addSubview:self.gameTableView];
//指定重用单元格的唯一标识
[self.gameTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
//确定单元格的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.marrName.count;
}
//单元格显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentity=@"cell";
//初始化并设置tableviewcell的类型
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentity];
//单元格主标题内容
cell.textLabel.text=self.marrName[indexPath.row];
//单元格副标题内容
cell.detailTextLabel.text=self.marrDownload[indexPath.row];
//单元格左边image图片
cell.imageView.image=[UIImage imageNamed:self.marrIcon[indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end