源码下载地址:http://download.youkuaiyun.com/detail/liu537192/8445185
效果图:
图1:图2:
图3:图4:
图5:图6:
核心代码:
//
// LiuJieHero.h
// 02-英雄展示
//
// Created by XinYou on 15-2-13.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface LiuJieHero : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *intro;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)heroWithDict:(NSDictionary *)dict;
@end
//
// LiuJieHero.m
// 02-英雄展示
//
// Created by XinYou on 15-2-13.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieHero.h"
@implementation LiuJieHero
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
// self.name = dict[@"name"];
// self.icon = dict[@"icon"];
// self.intro = dict[@"intro"];
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)heroWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
//
// LiuJieViewController.m
// 02-英雄展示
//
// Created by XinYou on 15-2-13.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieViewController.h"
#import "LiuJieHero.h"
@interface LiuJieViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *heros;
@end
@implementation LiuJieViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 设置UITableView的数据源,数据源需要实现UITableViewDataSource协议
self.tableView.dataSource = self;
// 设置UITableView的代理,代理需要实现UITableViewDelegate协议
self.tableView.delegate = self;
// 设置行高,这样设置后,每一行的高度都为60
// 如果需要设置成不同的行有不同的高度,请使用UITableViewDelegate代理的方法:
// (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
self.tableView.rowHeight = 60;
// 设置分隔线的颜色
// self.tableView.separatorColor = [UIColor redColor];
// 24bit(位)颜色:RGB 每一个颜色通道占据8个二进制位,每一个颜色通道的取值范围是[0,255]
// R:Red 8bit
// G:Green 8bit
// B:Blue 8bit
// #ff ff ff 白色
// #00 00 00 黑色
// #ff 00 00 红色
// #00 ff 00 绿色
// 32bit颜色:ARGB
// A:Alpha 8bit
// 假设我们需要设置一个红绿蓝分别为78、100、255的颜色,应该这样设置
self.tableView.separatorColor = [UIColor colorWithRed:78/255.0 green:100/255.0 blue:255/255.0 alpha:255/255.0];
// 设置UITableView的头部控件(可用于展示广告)
UIButton *topBtn = [[UIButton alloc] init];
topBtn.frame = CGRectMake(0, 20, 320, 40);
topBtn.backgroundColor = [UIColor blueColor];
[topBtn setTitle:@"这是广告" forState:UIControlStateNormal];
self.tableView.tableHeaderView = topBtn;
// 设置UITableView的底部控件(可用于显示"点击加载更多"按钮)
UIButton *bottomBtn = [[UIButton alloc] init];
bottomBtn.frame = CGRectMake(0, 0, 320, 40);
[bottomBtn setTitle:@"点击加载更多" forState:UIControlStateNormal];
[bottomBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
self.tableView.tableFooterView = bottomBtn;
}
/**
* 隐藏状态栏
*/
- (BOOL)prefersStatusBarHidden{
return YES;
}
- (NSArray *)heros{
if (_heros == nil) {
// 获取plist文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
// 加载plist文件中的数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tempHeros = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
LiuJieHero *hero = [LiuJieHero heroWithDict:dict];
[tempHeros addObject:hero];
}
_heros = tempHeros;
}
return _heros;
}
#pragma mark - 代理方法
/**
* @return 某一行对应的高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//
// // 设置成偶数行高度为80,奇数行高度为40
// if (indexPath.row % 2 == 0) {
// return 80;
// }
// return 40;
//}
#pragma mark - 数据源方法
/**
* @return 一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
/**
* @return 每组数据各有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.heros.count;
}
/**
* @return 每组数据对应的UITableViewCell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// static修饰局部变量,可以保证局部变量只分配一次存储空间(只初始化一次),提升性能
static NSString *ID = @"hero";
// 通过一个标识去缓存池中寻找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果没有找到可循环利用的cell,才创建新的cell(并且给新创建的cell弄一个标识,方便循环利用)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
LiuJieHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.imageView.image = [UIImage imageNamed:hero.icon];
cell.detailTextLabel.text = hero.intro;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;// 如图1所示
// cell.accessoryType = UITableViewCellAccessoryCheckmark;// 如图2所示
// cell.accessoryType = UITableViewCellAccessoryDetailButton;// 如图3所示
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;// 如图4所示
// cell.accessoryType = UITableViewCellAccessoryNone;// 如图5所示
// 如果系统自带的accessoryType不能满足我们的需要,我们还可以自定义accessoryType
// cell.accessoryView = [[UISwitch alloc] init];// 如图6所示
// 设置背景(背景view不用设置尺寸, backgroundView的优先级 > backgroundColor)
// cell.backgroundColor = [UIColor blueColor];
//
// UIImageView *bgView = [[UIImageView alloc] init];
// bgView.image = [UIImage imageNamed:@"buttondelete"];
// cell.backgroundView = bgView;
// 设置被选中的条目的背景
// UIImageView *selectedView = [[UIImageView alloc] init];
// selectedView.image = [UIImage imageNamed:@"buttondelete"];
// cell.selectedBackgroundView = selectedView;
return cell;
}
@end