通过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类:

  1. //  MyTableViewCell.h
  2. //  UITableViewCellDemo
  3. //
  4. //  Created by WildCat on 13-8-6.
  5. //  Copyright (c) 2013年 wildcat. All rights reserved.
  6. //
  7. #import <UIKit/UIKit.h>
  8. @interface MyTableViewCell : UITableViewCell
  9. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  10. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
  11. @property (copy,nonatomic) NSString *titleName;
  12. @property (copy,nonatomic) NSString *image;
  13. @end
  1. //
  2. //  MyTableViewCell.m
  3. //  UITableViewCellDemo
  4. //
  5. //  Created by WildCat on 13-8-6.
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.
  7. //
  8. #import "MyTableViewCell.h"
  9. @implementation MyTableViewCell
  10. @synthesize imageView;
  11. @synthesize titleLabel;
  12. @synthesize titleName;
  13. @synthesize image;
  14. -(void)setImage:(NSString *)image{
  15. self.imageView.image=[UIImage imageNamed:[image copy]];
  16. }
  17. -(void)setTitleName:(NSString *)titleName{
  18. self.titleLabel.text=[titleName copy];
  19. }
  20. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  21. {
  22. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  23. if (self) {
  24. // Initialization code
  25. }
  26. return self;
  27. }
  28. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  29. {
  30. [super setSelected:selected animated:animated];
  31. // Configure the view for the selected state
  32. }
  33. @end

ViewController类文件:

  1. //  ViewController.h
  2. //  UITableViewCellDemo
  3. //
  4. //  Created by WildCat on 13-8-6.
  5. //  Copyright (c) 2013年 wildcat. All rights reserved.
  6. //
  7. #import <UIKit/UIKit.h>
  8. @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
  9. @property (nonatomic,strong) UITableView *myTableView;
  10. @end
  1. //
  2. //  ViewController.m
  3. //  UITableViewCellDemo
  4. //
  5. //  Created by WildCat on 13-8-6.
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "MyTableViewCell.h"
  10. @interface ViewController ()
  11. @end
  12. @implementation ViewController
  13. @synthesize myTableView=_myTableView;
  14. #pragma mark -实现协议方法
  15. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
  16. return 1;
  17. }
  18. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  19. return 5;
  20. }
  21. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  22. CGFloat result = 40.0f;
  23. if ([tableView isEqual:self.myTableView]){
  24. result = 80.0f;
  25. }
  26. return result;
  27. }
  28. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  29. MyTableViewCell *cell;
  30. //定义CustomCell的复用标识,这个就是刚才在CustomCell.xib中设置的那个Identifier,一定要相同,否则无法复用
  31. static NSString *identifier = @"MyTableViewCell";
  32. //根据复用标识查找TableView里是否有可复用的cell,有则返回给cell
  33. cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
  34. //判断是否获取到复用cell,没有则从xib中初始化一个cell
  35. if (!cell) {
  36. //将Custom.xib中的所有对象载入
  37. NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
  38. //第一个对象就是CustomCell了
  39. cell = [nib objectAtIndex:0];
  40. }
  41. cell.image=@"1.jpeg";
  42. cell.titleName=@"wildcat的专栏 新浪微博:http://weibo.com/u/3202802157";
  43. return cell;
  44. }
  45. #pragma mark - Controller方法
  46. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  47. {
  48. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  49. if (self) {
  50. // Custom initialization
  51. }
  52. return self;
  53. }
  54. - (void)viewDidLoad
  55. {
  56. [super viewDidLoad];
  57. // Do any additional setup after loading the view.
  58. self.view.backgroundColor=[UIColor redColor];
  59. self.myTableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  60. self.myTableView.dataSource=self;
  61. self.myTableView.delegate=self;
  62. self.myTableView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  63. [self.view addSubview:self.myTableView];
  64. }
  65. - (void)viewDidUnload
  66. {
  67. [super viewDidUnload];
  68. // Release any retained subviews of the main view.
  69. self.myTableView=nil;
  70. }
  71. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  72. {
  73. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  74. }
  75. @end

运行结果:

通过xib自定义UITableViewCell的更多相关文章

  1. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  2. 新手教程之使用Xib自定义UITableViewCell

    新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...

  3. 【转】iOS 通过xib自定义UITableViewCell【原创】

    原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...

  4. 【swift学习笔记】三.使用xib自定义UITableViewCell

    使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...

  5. 用xib自定义UITableViewCell的注意事项——重用

    问题的提出: 有时候我们经常需要自定义tableView的cell,当cell里面的布局较为复杂时往往舍弃纯代码的方式而改用xib的方式进行自定义.当我们用纯代码的方式布局cell时,往往会在cell ...

  6. 用xib自定义UITableViewCell

    1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 #import "Mo ...

  7. IOS学习之路七(通过xib自定义UITableViewCell)

    一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...

  8. 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

    自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...

  9. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

随机推荐

  1. Apache & WebDav 配置(二)

    (二)WebDav配置 & put脚本编辑! WebDav 配置 ---------------- # 1. 修改了两个配置文件 # 2. 创建web访问用户的用户名和口令 # 3. 创建了两 ...

  2. Roundabout for jQuery

    效果图: Roundabout是一个转换静态HTML元素结构为交互式播放区域的jQuery插件(而且并不仅仅是一个转盘,还有许多的形状) 首先你要下载好Jquery.min.js,和Jquery-Ro ...

  3. vistual studio 2012 安装失败,提示Microsoft Vistual Studio 2012 Devenv找不到元素,等错误信息

    在安装vistual studio 2012过程中,出现安装失败,提示Microsoft Vistual Studio 2012 Devenv找不到元素,等错误信息 解决方法是更新相应的server补 ...

  4. IIS7 URL Rewrite 用法实例

    原文:IIS7 URL Rewrite 用法实例 很友好的URL地址,使访问的人很容易记住.要求你的用户记住" http://www.contoso.com/article.aspx?id= ...

  5. MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串

    原文:MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串 如何让视图通过某种途径,把符合日期格式的字符串放到路由中,再传递给类型为DateTime的控制 ...

  6. iOS--Swift开发中的单例设计模式

    最近在开发一个小的应用,遇到了一些Objective-c上面常用的单例模式,但是swift上面还是有一定区别的,反复倒来倒去发现不能按常理(正常的oc to swift的方式)出牌,因此搜索了一些帖子 ...

  7. 一步一步写算法(之挑选最大的n个数)

    原文:一步一步写算法(之挑选最大的n个数) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 从一堆数据中挑选n个最大的数,这个问题是网上流传的 ...

  8. Linq to Sql:N层应用中的查询(下) : 根据条件进行动态查询

    原文:Linq to Sql:N层应用中的查询(下) : 根据条件进行动态查询 如果允许在UI层直接访问Linq to Sql的DataContext,可以省去很多问题,譬如在处理多表join的时候, ...

  9. 【强烈强烈推荐】《ORACLE PL/SQL编程详解》全原创(共八篇)--系列文章导航

    原文:[强烈强烈推荐]<ORACLE PL/SQL编程详解>全原创(共八篇)--系列文章导航 <ORACLE PL/SQL编程详解> 系列文章目录导航 ——通过知识共享树立个人 ...

  10. POJ 3071-Football(可能性dp)

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3145   Accepted: 1591 Descript ...