WXHL 学习总结(七)
2015-08-27 08:47
267 查看
通过一个多月的学习,UI基础已经讲完,能做出一些简单的小案例,从这周开始,就着手开始真正的第一个小项目了。还是有一定的收获的
下面是一些简单的表视图的应用案例:
1 微博 :练习使用自定义单元格,主要是计算好每个单元格的高度,以及单元格内每个控件的frame,因为有些是会员,有的有插图,有的没插图,所以计算略显复杂
#pragma mark - 复写get方法
-(NSArray *)weibos{
if (_weibos == nil) {
//获取plist文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
//将数据放进数组
self.weibos = [NSArray arrayWithContentsOfFile:path];
//定义一个可变数组 存放模型
NSMutableArray *models = [NSMutableArray array];
//字典转模型
for (NSDictionary *dict in self.weibos)
{
WXWeibo *model = [WXWeibo weiboWithDict:dict];
[models addObject:model];
}
_weibos = models;
}
return _weibos;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.frame = CGRectMake(0, 64, 375, 603);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.weibos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 1 获取模型对象
WXWeibo *weibo = self.weibos[indexPath.row];
// 2 创建单元格
static NSString *identifier = @"weibo_cell";
WXWeiboCell *cell = [[WXWeiboCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// 3 给单元格赋值
cell.weibo = weibo;
// 4 返回单元格
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
WXWeibo *weibo = self.weibos[indexPath.row];
NSString *text = weibo.text;
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(350, 1000)];
//如果有图片 加上图片的高度
if (weibo.picture) {
return size.height + 40 + 35 + 100;
}else{
//没有图片 返回正文高度 头像高度 之和
return size.height + 30 +35;
}
}
效果图:
2. 汽车列表、LOL英雄列表 : 练习单元格的使用,以及重用问题
汽车展示:分组展示每个系列的车 右侧通过方法添加索引
英雄展示:可以点击单元格,修改英雄的名字
效果图:
下面是一些简单的表视图的应用案例:
1 微博 :练习使用自定义单元格,主要是计算好每个单元格的高度,以及单元格内每个控件的frame,因为有些是会员,有的有插图,有的没插图,所以计算略显复杂
#pragma mark - 复写get方法
-(NSArray *)weibos{
if (_weibos == nil) {
//获取plist文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
//将数据放进数组
self.weibos = [NSArray arrayWithContentsOfFile:path];
//定义一个可变数组 存放模型
NSMutableArray *models = [NSMutableArray array];
//字典转模型
for (NSDictionary *dict in self.weibos)
{
WXWeibo *model = [WXWeibo weiboWithDict:dict];
[models addObject:model];
}
_weibos = models;
}
return _weibos;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.frame = CGRectMake(0, 64, 375, 603);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.weibos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 1 获取模型对象
WXWeibo *weibo = self.weibos[indexPath.row];
// 2 创建单元格
static NSString *identifier = @"weibo_cell";
WXWeiboCell *cell = [[WXWeiboCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// 3 给单元格赋值
cell.weibo = weibo;
// 4 返回单元格
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
WXWeibo *weibo = self.weibos[indexPath.row];
NSString *text = weibo.text;
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(350, 1000)];
//如果有图片 加上图片的高度
if (weibo.picture) {
return size.height + 40 + 35 + 100;
}else{
//没有图片 返回正文高度 头像高度 之和
return size.height + 30 +35;
}
}
效果图:
2. 汽车列表、LOL英雄列表 : 练习单元格的使用,以及重用问题
汽车展示:分组展示每个系列的车 右侧通过方法添加索引
英雄展示:可以点击单元格,修改英雄的名字
效果图:
相关文章推荐
- oc 点语法 / KVC /readwrite/readonly/
- 解决趋势OfficeScan11自动显示隐藏文件等问题
- 黑马程序员--类的声明和实现
- 1068. Find More Coins (30)搜索题
- Access restriction: The type BASE64Encoder is not accessible due to restrict on required library
- HDOJ 1162 Eddy's Picture (最小生成树)
- Java中方法重载时注意点
- HDU 5093 Battle ships(二分图匹配)
- 数组元素是 字符串 类型,并且包含 -等 特殊字符
- Win10正式上市整整一个月时间:最新全球安装量已超7500万
- ATS插件channel_stats源码分析解读
- 【Cactus仙人掌图】仙人掌基础知识学习笔记
- java中i++和++i与c里的区别
- myeclipse8.5破解文档
- 关于C++、PHP和Swoole
- Eclipse常用快捷键
- Office2013 分享
- entity framework 6 通用数据类
- 序列化和反序列化时的注意点
- 移植最新内核Linux4.1.6到TQ2440开发板教程(一)