IOS学习之路七(通过xib自定义UITableViewCell)
一、新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCellDemo,则完成后自动生成代码视图如下图:
二。新建一个UITableViewCell文件:
三。Add---New Files----User Interface-----Empty XIB
创建一个空的 MyTableViewCell.xib 文件,记住,XIB的名称一定要跟 签名的类的名称一致,也就是一模一样。
一定要选 Empty XIB类型,如果不是选的这个,那么创建的XIB里面的已经存在的那个UIView将不能调整高度,它的高度固定死了。
4.在xib中拖入一个Table View Cell 和一个label 一个imageView ,并于MyTableViewCell中连接如下图:
五。这样,就可以往这个新添加的View里面添加我们自己的个性化控件了,这个View就是我们的Cell的模板了。这个过程跟普通的XIB一样,没有什么特别的。
那么如何在代码中使用这个MyTableViewCell呢?
代码如下:
MyTableViewCell类:
- // MyTableViewCell.h
- // UITableViewCellDemo
- //
- // Created by WildCat on 13-8-6.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface MyTableViewCell : UITableViewCell
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
- @property (copy,nonatomic) NSString *titleName;
- @property (copy,nonatomic) NSString *image;
- @end
- //
- // MyTableViewCell.m
- // UITableViewCellDemo
- //
- // Created by WildCat on 13-8-6.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import "MyTableViewCell.h"
- @implementation MyTableViewCell
- @synthesize imageView;
- @synthesize titleLabel;
- @synthesize titleName;
- @synthesize image;
- -(void)setImage:(NSString *)image{
- self.imageView.image=[UIImage imageNamed:[image copy]];
- }
- -(void)setTitleName:(NSString *)titleName{
- self.titleLabel.text=[titleName copy];
- }
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- // Initialization code
- }
- return self;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated
- {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- @end
ViewController类文件:
- // ViewController.h
- // UITableViewCellDemo
- //
- // Created by WildCat on 13-8-6.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
- @property (nonatomic,strong) UITableView *myTableView;
- @end
- //
- // ViewController.m
- // UITableViewCellDemo
- //
- // Created by WildCat on 13-8-6.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import "ViewController.h"
- #import "MyTableViewCell.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- @synthesize myTableView=_myTableView;
- #pragma mark -实现协议方法
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return 5;
- }
- - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- CGFloat result = 40.0f;
- if ([tableView isEqual:self.myTableView]){
- result = 80.0f;
- }
- return result;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- MyTableViewCell *cell;
- //定义CustomCell的复用标识,这个就是刚才在CustomCell.xib中设置的那个Identifier,一定要相同,否则无法复用
- static NSString *identifier = @"MyTableViewCell";
- //根据复用标识查找TableView里是否有可复用的cell,有则返回给cell
- cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
- //判断是否获取到复用cell,没有则从xib中初始化一个cell
- if (!cell) {
- //将Custom.xib中的所有对象载入
- NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
- //第一个对象就是CustomCell了
- cell = [nib objectAtIndex:0];
- }
- cell.image=@"1.jpeg";
- cell.titleName=@"wildcat的专栏 新浪微博:http://weibo.com/u/3202802157";
- return cell;
- }
- #pragma mark - Controller方法
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.view.backgroundColor=[UIColor redColor];
- self.myTableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
- self.myTableView.dataSource=self;
- self.myTableView.delegate=self;
- self.myTableView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
- [self.view addSubview:self.myTableView];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- self.myTableView=nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end
运行结果:
IOS学习之路七(通过xib自定义UITableViewCell)的更多相关文章
- 【swift学习笔记】三.使用xib自定义UITableViewCell
使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...
- IOS学习之路七(使用 Operation 异步运行任务)
在 application delegate 头文件(.h)中声明一个 operation 队列和两个 invocation operations: #import <UIKit/UIKit.h ...
- iOS学习之路十三(动态调整UITableViewCell的高度)
大概你第一眼看来,动态调整高度是一件不容易的事情,而且打算解决它的第一个想法往往是不正确的.在这篇文章中我将展示如何使图表单元格的高度能根据里面文本内容来动态改变,同时又不必子类化UITableVie ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路
在不知不觉当中已经写了21篇内容 其实一开始是没有想些什么东西的 只是买了Air后 感觉用着挺舒服的,每天可以躺在床上,就一台笔记本,不用网线,不用电源,不用鼠标,不用键盘,干干脆脆的就一台笔记本. ...
- 【转】iOS 通过xib自定义UITableViewCell【原创】
原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- 通过xib自定义UITableViewCell
通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...
- 新手教程之使用Xib自定义UITableViewCell
新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...
随机推荐
- crawler_httpurlconnection_自动编码识别
核心思想: 1:从响应头中读取 [命中解流准确率最高] 2:如果响应头中没有,打开流从源码中读取,[取舍,如果有一般在前30行会有,前100行中寻找] 3:如果还没有,根据字节码code位置,字符识别 ...
- JSON多层数据添加与访问
最近项目中有要用到,JSON的多层数据对象,相当是一个json格式数组里面嵌套一个json对象吧,至于我为什么要用到这个呢,引入业务场景: 两组数据 1: user_id user_h ...
- Scriptcase演示程序,现在,他们使用SC多么简单的开发系统
Scriptcase它内置了一批成熟的业务系统,我们的系统的一部分已经完成和功能微调,在互联网上.检查每个人Scriptcase如何简单可以加速的功能模块的发展. 访问 http://www.phps ...
- 【百度地图API】——如何让标注自动呈现在最佳视野内
原文:[百度地图API]--如何让标注自动呈现在最佳视野内 摘要: “我有一堆标注,不规则的散落在地图的各个地方,我想把它们展示在一个最佳视野中,怎么办呢?”一位API爱好者咨询道. -------- ...
- Swift语言指南(七)--语言基础之布尔值和类型别名
原文:Swift语言指南(七)--语言基础之布尔值和类型别名 布尔值 Swift有一个基本布尔类型,叫做布尔(bool),布尔值又称逻辑值(logical),因为它只能为真(true)或假(false ...
- python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气:
下面是接口xml格式数据: <rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo=& ...
- CentOS采用grub进 single状态
当系统文件错误,有可能无法进入系统.或者您忘记了原来的系统password. 用这种方法可以,进single状态,编辑系统启动文件或更改的错误password. 1.重新启动系统. 2.按" ...
- 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。
原文:在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持. WAMPServer可以让开发者在Windows系统下快速搭建WAMP环境,它支持多版本的Apach ...
- SQL Server调优系列基础篇(常用运算符总结)
原文:SQL Server调优系列基础篇(常用运算符总结) 前言 上一篇我们介绍了如何查看查询计划,本篇将介绍在我们查看的查询计划时的分析技巧,以及几种我们常用的运算符优化技巧,同样侧重基础知识的掌握 ...
- 通过扩展改善ASP.NET MVC的验证机制[实现篇]
原文:通过扩展改善ASP.NET MVC的验证机制[实现篇] 在<使用篇>中我们谈到扩展的验证编程方式,并且演示了本解决方案的三大特性:消息提供机制的分离.多语言的支持和多验证规则的支持, ...