iOS开发之高级视图—— UITableView(三)读取plist文件

       此处实现了一个读取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


       效果图如下:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值