IOS开发学习笔记030-xib实现淘宝界面
使用xib文件实现界面,然后通过模型更新数据。
1、使得控制器继承自UITableViewController
2、创建xib文件,实现界面如下:一个UIImageView,两个lable
3、新建一个封装类NewCell,封装对xib界面的操作
4、新建一个模型类Shops对数据进行更新,读取字典数据到类中
5、在控制器中对模型数据进行操作,将结果显示到view中
1、使得控制器继承自UITableViewController
更改main.storyboard的主界面是UITableViewController的class为SLQViewController
2、创建xib文件,实现界面如下:一个UIImageView,两个lable
指定NewCell的class为下面新建的NewCell类,这样可以拖线添加属性和方法。
3、新建一个封装类NewCell,封装对xib界面的操作
头文件
//
// NewCell.h
// UITableViewCell-xib实现
//
// Created by Christian on 15/5/21.
// Copyright (c) 2015年 slq. All rights reserved.
// #import <UIKit/UIKit.h>
@class Shops; // 引用声明
@interface NewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *icon; // 图标
@property (weak, nonatomic) IBOutlet UILabel *name; // 标题
@property (weak, nonatomic) IBOutlet UILabel *desc; // 描述 @property (nonatomic,strong) Shops *shop; // shop对象 + (id)newCell; // 返回NewCell对象
+ (NSString *)ID; // 返回标识
+ (CGFloat)cellHeight; // 返回cell高度
@end
源文件
//
// NewCell.m
// UITableViewCell-xib实现
//
// Created by Christian on 15/5/21.
// Copyright (c) 2015年 slq. All rights reserved.
// #import "NewCell.h"
#import "Shops.h" @implementation NewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
} - (void)awakeFromNib
{
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated]; // Configure the view for the selected state
}
// 返回NewCell对象
+ (id)newCell
{
// 加载xib文件
return [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:nil options:nil][];
}
// 重写setShop方法
- (void)setShop:(Shops *)shop
{
// 标题
self.name.text = shop.name;
// 图片
self.icon.image = [UIImage imageNamed:shop.icon];
// 描述
self.desc.text = shop.desc;
}
// 返回标识
+ (NSString *)ID
{
return @"CELL";
}
// 返回行高度
+ (CGFloat)cellHeight
{
return ;
}
@end
4、新建一个模型类Shops对数据进行更新,读取字典数据到类中
//
// Shops.h
// UITableViewCell-xib实现
//
// Created by Christian on 15/5/21.
// Copyright (c) 2015年 slq. All rights reserved.
// #import <Foundation/Foundation.h> @interface Shops : NSObject
@property (nonatomic,copy) NSString *name; // 标题
@property (nonatomic,copy) NSString *desc; // 描述
@property (nonatomic,copy) NSString *icon; // 图标 + (id)shopWithDict:(NSDictionary *)dict; // 返回shop对象
- (id)initWithDict:(NSDictionary *)dict; // 自定义构造方法 @end
实现文件如下:
//
// Shops.m
// UITableViewCell-xib实现
//
// Created by Christian on 15/5/21.
// Copyright (c) 2015年 slq. All rights reserved.
// #import "Shops.h" @implementation Shops // 返回一个Shop对象
- (id)initWithDict:(NSDictionary *)dict
{
// 父类init
if(self = [self init])
{
// 赋值
self.name = dict[@"name"];
self.icon = dict[@"icon"];
self.desc = dict[@"desc"];
}
// 返回self
return self;
} + (id)shopWithDict:(NSDictionary *)dict
{
return [[Shops alloc] initWithDict:dict];
}
@end
5、在控制器中对模型数据进行操作,将结果显示到view中
主要是这两个方法:设置行和行内容
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1、取出循环池中得cell
NewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NewCell ID]];
// 2、如果cell不空
if(cell == nil)
{
cell = [NewCell newCell];
}
// 3、设置cell内容
cell.shop = _data[indexPath.row];
return cell;
}
还有plist文件的加载
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_data = [NSMutableArray array];
//加载数据
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil]];
for (NSDictionary *arr in array)
{
[_data addObject:[Shops shopWithDict:arr]];
}
// 设置行高度
self.tableView.rowHeight = [NewCell cellHeight];
}
源代码:http://pan.baidu.com/s/1kTswBjD
欢迎关注个人订阅号:
IOS开发学习笔记030-xib实现淘宝界面的更多相关文章
- iOS开发学习笔记:基础篇
iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...
- ios开发学习笔记(1)
objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...
- IOS开发学习笔记042-UITableView总结2
一.自定义非等高的cell 如常见的微博界面,有的微博只有文字,有的有文字和图片.这些微博的高度不固定需要重新计算. 这里简单说一下几种方法.前面的步骤和设置等高的cell一样.现在来 ...
- IOS开发学习笔记029-反选、全选、删除按钮的实现
还是在上一个程序的基础上进行修改 1.反选按钮 2.全选按钮 3.删除按钮 4.其他代码优化 1.反选按钮 反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_de ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS开发学习笔记
1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...
- (ios开发学习笔记一)ios项目文件结构
转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...
- IOS开发学习笔记017-第一个IOS应用
第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...
- IOS开发学习笔记041-UITableView总结1
一.UITableView的常用属性 1.分割线 // 分割线 self.tableView.separatorColor = [UIColorredColor]; // 隐藏分割线 self.tab ...
随机推荐
- Python开发环境Wing IDE设置Python路径详解
在使用Wing IDE的时候,Python源代码取决于PYTHONPATH(无论是外部或通过内部改变sys.path系统设置),用户需要将路径设置到Wing IDE中. 这个值可以从项目菜单和工具栏的 ...
- mathtype 章节号 Equation Chapter 1 Section 1 的去除
mathtype 章节号 Equation Chapter 1 Section 1 的去除 转:http://hi.baidu.com/17ximm/blog/item/2882413e92fc96c ...
- 【Quartus错误】Internal Error: Sub-system: AMERGE
错误内容:Internal Error: Sub-system: AMERGE, File: /quartus/atm/amerge/amerge_kpt_op.cpp, Line: 220 解决方案 ...
- String和string
String和string的区别 从位置讲: 1.String是.NET Framework里面的String,小写的string是C#语言中的string 2.如果把using ...
- (转) HTTP Request header
HTTP Request header 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基 ...
- 新版graylog2安装过程
Graylog是一个开源的 log 收容器,背后的储存是搭配 mongodb,而搜寻引擎则由 elasticsearch 提供.以前版本主要有两个部分集合而成 server 与 web interfa ...
- IOS 响应者链条 and UIGestureRecognizer 手势识别器)
一次完整的触摸事件的传递响应的过程 UIAppliction --> UIWiondw -->递归找到最适合处理事件的控件 控件调用touches方法-->判断是否实现touches ...
- 2017.12.24 Java序列化你不知道的事(二)
1 序列化允许重构 序列化允许一定数量的类变种,甚至重构之后也是如此,ObjectInputStream 仍可以很好地将其读出来. Java Object Serialization 规范可以自动管理 ...
- python_39_变量补充
#使用eval()函数计算字符串中的有效表达式,并返回结果 a='1+3' print(eval(a)) b='''{ '闵行': { '人民广场': { '炸鸡店': {}, }, } ''' pr ...
- Linux内核参数min_free_kbytes
1. min_free_kbytes 先看官方解释: This is used to force the Linux VM to keep a minimum number of kilobytes ...