iphone开发之表格组件UITableView的使用(四) 通过加载plist文件展示单组数据

1、单元格样式UITableViewCellStyleDefault 即默认样式是不显示小的Label简介的即不显示detailTextLabel上的Text的。
UITableViewCellStyleSubtitle样式:都显示,并且detailTextLabel上的Text作为小字体简介放在textLabel大字体标签下边。
UITableViewCellStyleValue1样式:都显示。但是detailTextLabel上的Text放在textLabel的右面。
UITableViewCellStyleValue2样式:不显示图片框。
2、去掉状态栏的方法:添加以下方法。
-(BOOL)prefersStatusBarHidden
{
return YES;
}
3、如果想在单元格右边显示一个组件,方法如下:
(1)系统提供的的控件,如箭头,详细信息按钮,等可以用UITableViewCell类的accessoryType属性进行设置。例如:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加箭头
(2)可以自定义单元格右边的accessory。如:
cell.accessoryView = [[UISwitch alloc] init]; // 添加按钮
4、统一设置UITableView的行高。
1>(在每一行行高都相同的情况下)
在viewDidLoad方法中添加以下语句即可:
self.tableView.rowHeight = 60;
2> 对于每行行高都不一样的情况,无法通过tableView.rowHeight来实现,此时只能通过一个代理方法实现。具体如下:
先让当前控制器遵守UITableViewDelegate代理协议。然后添加方法如下即可:
-(CGFloat)tableView :(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
int rowNum = indexPath.row;
if(rowNum %2 ==0){
return 60;
}else{
return 100;
}
}

代码验证:
(1)(新建或加载plist文件)
(2)打开plist文件,查看对象字典属性。
(3)根据字典的各个键与对应值的类型新建model类
(4)为控制器添加数组属性,并重写数组属性的get方法实现plist文件的懒加载。
(5)添加UITableView组件属性,因为默认就是单组风格。不用特地设置。在viewDidLoad方法中进行属性设置。
(6)让控制器类遵守UITableViewDataSource数据源协议和UItableViewDelegate代理 协议,并设置自身的代理对象与数据源对象为当前控制器对象。即self.tableView.delegate = self;
self.tableView.dataSource=self;
(7)添加数据源协议中的方法

、、、、

plist文件如下:


将相应图片资源拉进工程中一个自设的文件夹,在Supporting files下新建model类如下:

参照文件中字典的属性,编辑Hero.h如下:

//
//  Hero.h
//  LOL英雄榜UITableView加载plist展示单组数据
//
//  Created by apple on 15/8/31.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Hero : NSObject
@property (nonatomic, strong) NSString *icon;
@property (nonatomic, strong) NSString *intro;
@property (nonatomic, strong) NSString *name;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)heroWithDict:(NSDictionary *)dict;
@end
编辑Hero.m如下:

//
//  Hero.m
//  LOL英雄榜UITableView加载plist展示单组数据
//
//  Created by apple on 15/8/31.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "Hero.h"

@implementation Hero
-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
//        self.name = dict[@"name"];
//        self.icon = dict[@"icon"];
//        self.intro = dict[@"intro"];
        [self setValuesForKeysWithDictionary:dict];  // KVC 
    }
    return self;
}
+(instancetype)heroWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

@end
编辑控制器的.h文件如下:

//
//  ViewController.h
//  LOL英雄榜UITableView加载plist展示单组数据
//
//  Created by apple on 15/8/31.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *array;
@end
编辑控制器的.m文件如下:

//
//  ViewController.m
//  LOL英雄榜UITableView加载plist展示单组数据
//
//  Created by apple on 15/8/31.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"
#import "Hero.h"
#define WIDTH    [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height



@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //  添加UITableView控件
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;  // 取消竖直滑动条
    self.tableView.rowHeight = 70;  // 只对于所有行高度都相同的情况下
    [self.view addSubview:self.tableView];
    
}

// 重写数组的get方法实现懒加载
-(NSArray *)array
{
    if (_array == nil) {
        
        //查找plist文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        
        // 根据路径读取plist文件到一个数组
        NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
        
         // 新建空的一个专门存放model对象的可变数组
        NSMutableArray *arrayModel = [NSMutableArray array];
        
        // 遍历从文件读取存放所有字典的数组,逐个转成model对象存放到可变数组中
        for (NSDictionary * dict  in   arrayDict){
            // 新建一个model对象
            Hero *hero = [Hero heroWithDict:dict];
            [arrayModel addObject:hero];
        }
        _array = arrayModel;
    }
    
    return _array;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 取消状态栏
-(BOOL)prefersStatusBarHidden
{
    return YES;
}

 // 利用UITableViewDelegate代理协议中的方法,即使行高不相同的也行
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row %2 == 0) {
        return 60;
    }else{
    
        return  80;
    }

}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 新建指定格式的单元格对象
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
   // 获取model数据
    Hero *hero = self.array[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    
    //  添加箭头
    /*   accessoryType  的枚举值如下:
     UITableViewCellAccessoryNone,                   // don't show any accessory view
     UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
     UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks
     UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track
     UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks*/
    
    if(indexPath.row <1){
    cell.accessoryType = UITableViewCellAccessoryNone; // 默认状态下不显示任何组件
    }else if(indexPath.row<2){
        cell.accessoryType =  UITableViewCellAccessoryDetailButton ; //显示详细信息按钮
    }else if (indexPath.row<3){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;  // 添加选择按钮
    }else if (indexPath.row <4){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加箭头按钮
    }else if (indexPath .row<5){
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // 同时添加箭头与详细信息的按钮
    }else if (indexPath.row <6 ){
        cell.accessoryView = [[UISwitch alloc] init];
    }else
        cell.accessoryView = [[UIButton alloc] init];
    
    
    return cell;
}

@end
运行结果如下:












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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值