此处实现了一个读取plist文件,并把文件里面的数据加载到UITableView列表上。
本例子需要创建一个plist文件,此处不详细描述文件创建过程,仅给出文件内容。
Heroes.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!--
Heroes.plist
UITableViewPListApp
Created by Apple on 16/5/25.
Copyright (c) 2016年 Apple. All rights reserved.
-->
<plist version="1.0">
<array>
<string>迅捷斥候</string>
<string>放逐之刃</string>
<string>德邦总管</string>
<string>皮城执法官</string>
<string>铸星龙王</string>
<string>未来守护者</string>
</array>
</plist>
ViewController.m
//
// ViewController.m
// UITableViewPListApp
//
// Created by Apple on 16/5/25.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
NSArray* array;
- (void)viewDidLoad {
[super viewDidLoad];
//获取一个plist文件的路径,方便操作
NSString* path = [[NSBundle mainBundle] pathForResource:@"Heroes" ofType:@"plist"];
//根据给定的plist文件路径获取plist文件中的内容,创建一个对应的对象
array = [[NSArray alloc] initWithContentsOfFile:path];
UITableView* tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
//注册可重用的Cell 到tableView上
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
[self.view addSubview:tableView];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//当程序运行的时候自己判断cell是否有可以重复使用的,如果没有将自动创建一个cell,并绑定到cellID标识符上,方便下次重复使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
@end
效果图如下: