UITableView

//  ViewController.h文件app

 

#import <UIKit/UIKit.h>atom

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//遵循協議代理

 

@property (strong,nonatomic) UITableView *gameTableView;圖片

 @property (strong,nonatomic) NSMutableArray *marrName;ci

@property (strong,nonatomic) NSMutableArray *marrIcon;it

@property (strong,nonatomic) NSMutableArray *marrDownload;io

 

@endtable

 

//  ViewController.m文件class

#import "ViewController.h"import

 

@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

相關文章
相關標籤/搜索