通过xib自定义UITableViewCell
通过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
运行结果:
通过xib自定义UITableViewCell的更多相关文章
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- 新手教程之使用Xib自定义UITableViewCell
新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...
- 【转】iOS 通过xib自定义UITableViewCell【原创】
原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...
- 【swift学习笔记】三.使用xib自定义UITableViewCell
使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...
- 用xib自定义UITableViewCell的注意事项——重用
问题的提出: 有时候我们经常需要自定义tableView的cell,当cell里面的布局较为复杂时往往舍弃纯代码的方式而改用xib的方式进行自定义.当我们用纯代码的方式布局cell时,往往会在cell ...
- 用xib自定义UITableViewCell
1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 #import "Mo ...
- IOS学习之路七(通过xib自定义UITableViewCell)
一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...
- 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
随机推荐
- idea类似eclipse鼠标技巧java api信息
版权声明:本文博客原创文章,博客,未经同意,不得转载.
- hdu(2062)-Subset sequence 组合数学
意甲冠军:查找集合{1,2,3...n}第一m一个排列子. 收集的线索所行的大小. 例两个元素的排列子集合按字典树排列是:{1},{1,2},{2},{2,1}: 解法:一个一个元素来确定,每次把剩余 ...
- 针对不同手机系统的LBS地图定位解决方案
原文:针对不同手机系统的LBS地图定位解决方案 摘要: 针对目前的三种手机系统:Android安卓.S60塞班.IOS苹果,做出的三种不同的手机地图应用解决方案. 查阅了多数地图API对手机的支持情况 ...
- .net 伪静态、真静态 (mvc)
MVC 模式下的伪静态: 通过路由就可以搞定 首先说下路由规则,允许多个路由规则,会从上之下寻找匹配 伪静态: 如果只写一个路由 那么 所有页面都必须是 html 结尾. 如果再加一个路由 就都支持了 ...
- Excel 删除所有错误公式
当前工作表的话可以F5-定位-公式-错误值 来选中所有含错误值的单元格,然后按delete删除. 多表的话没办法了,因为不能跨工作表多重选中,只能一页页的删,或者用vba编个宏来解决
- sgu139Help Needed!推断15数码是否有解,以及推断N数码是否有解的推论
是这种,要你推断一个15数码是否有解. 我不会,找了这样一个方法. 将16个数按出现顺序存放在一维数组里面, 然后累加每一个数的逆序对数目, 还要加上0到终态的曼哈顿距离,得到一个数x. 因为最后的状 ...
- Fckeditor用法
试Fckeditor版本号:2.6.3 眼下Fckeditor仅仅能用于基于ie内核的浏览器,假设要使用于chrome等浏览器,请使用ckeditor. 详细用法: 1.将解压后的fckeditor整 ...
- 使用collectd与visage收集kvm虚拟机性能实时图形
软件功能: 通过collectd软件来监控收集kvm虚拟机的性能数据,包含cpu,memory.磁盘IO.网络流量等 通过visage软件将收集到的数据绘制图形. 安装: 系统环境:ubuntu12. ...
- LibVLC video controls
原文 http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__video.html VLC 3.0.0-git ...
- AMDBarUtility Update Ditection Page
Current version is : #################### #060901# #################### DO NOT REPLY!!!