UITableViewCell:自定义的单元格,可以在xib中创建单元格,也可以在storyBorad中创建单元格。有四种创建方式

<1>在storyBorad中创建的单元格,它是静态的单元格,单元格一开始就存在,可以直接根据自定义的重用标识名加载使用;
<2>当然,storyBorad中单元格也可以关联一个自定义的类,这个类必须是继承UITableViewCell,这种情况下,直接根据自定义的重用标识名加载使用也是可以的。
<3>在xib中创建的单元格,如果直接通过bundel的loadNibNme的方法加载,也可以直接根据重用标识符加载使用;
<4>当然,xib文件中的单元格可以关联一个自定义的类,这个类必须是继承UITableViewCell,这种情况下,如果直接根据自定义的重用标识符加载使用是行不通的,因为此时代理的方法没有对单元格对象进行初始化,此时,需要对创建单元格对象的过程封装到自己关联的类中进行,即一个创建的单元格的类方法用来加载xib文件,一个类对象的实例方法,用来设置单元格中属性。
 
  
  这是一个类似于联系人表格的实例,有姓名和图像,以下四种方式都可以实现:
 
  方法一:直接在storyBoard中创建单元格并直接加载,自定义的单元格位置一个UITableView的上面
  需要设置单元格的重用标识符identifier:
代码如下:
   为初始化数据创建的一个类:

 #import <Foundation/Foundation.h>

 @interface Contact : NSObject
@property (copy,nonatomic)NSString *name;
@property (copy,nonatomic)NSString *faceName;
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName;
@end
 #import "Contact.h"

 @implementation Contact
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName
{
self = [super init];
if(self)
{
_name = [name copy];
_faceName = [faceName copy];
}
return self;
}
@end

  在视图控制器中完成代码:(需要用tag获取单元格的属性控件)

 #import "ViewController.h"
#import "Contact.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *contacts;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//初始化数据
self.contacts = [NSMutableArray arrayWithCapacity:];
for(int i=; i<; i++)
{
Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+] andFaceName:[NSString stringWithFormat:@"%d.png",i]];
[self.contacts addObject:conatct];
} //设置tableView的数据源
self.tableView.dataSource = self;
} #pragma mark -tableView的数据源方法
//每一组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contacts.count;
}
//设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"myCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.设置单元格对象的内容
Contact *contact = [self.contacts objectAtIndex:indexPath.row];
UILabel *label = (UILabel*)[cell viewWithTag:];
label.text = contact.name;
UIImageView *imageView = (UIImageView*)[cell viewWithTag:];
[imageView setImage:[UIImage imageNamed:contact.faceName]];
return cell;
} @end

   方法二:直接在storyBoard中创建单元格并关联自定义的类并直接加载,自定义的单元格位置一个UITableView的上面

 
  需要设置单元格的重用标识符identifier
 
 
  将单元格与对应的自定义类关联
 
  代码如下:
为初始化创建的一个类:
#import <Foundation/Foundation.h>

@interface Contact : NSObject
@property (copy,nonatomic)NSString *name;
@property (copy,nonatomic)NSString *faceName;
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName;
@end #import "Contact.h" @implementation Contact
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName
{
self = [super init];
if(self)
{
_name = [name copy];
_faceName = [faceName copy];
}
return self;
}
@end

  与单元格关联的自定义的类,关联单元格的属性控件(不需要再用tag获取了,直接用self.获取)

  还是在视图控制器中完成加载:

 #import "ViewController.h"
#import "Contact.h"
#import "myTableViewCell.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *contacts;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//初始化数据
self.contacts = [NSMutableArray arrayWithCapacity:];
for(int i=; i<; i++)
{
Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+] andFaceName:[NSString stringWithFormat:@"%d.png",i]];
[self.contacts addObject:conatct];
} //设置tableView的数据源
self.tableView.dataSource = self;
} #pragma mark -tableView的数据源方法
//每一组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contacts.count;
}
//设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"myCell";
myTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.设置单元格对象的内容
Contact *contact = [self.contacts objectAtIndex:indexPath.row];
cell.label.text = contact.name;
[cell.imgView setImage:[UIImage imageNamed:contact.faceName]];
return cell;
} @end

  方法三:在xib文件中创建单元格,然后再视图控制器中直接加载使用

  首先在storyBoard中添加一个UITableView

  然后在已经创建好的MyCell.xib中创建自定义的单元格为:

  设置该单元格的重用标识符identifier:

   创建一个联系人初始化的类:

 #import <Foundation/Foundation.h>

 @interface Contact : NSObject
@property (copy,nonatomic)NSString *name;
@property (copy,nonatomic)NSString *faceName;
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName;
@end #import "Contact.h" @implementation Contact
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName
{
self = [super init];
if(self)
{
_name = [name copy];
_faceName = [faceName copy];
}
return self;
}
@end

   还是在视图控制器中完成加载:

 #import "ViewController.h"
#import "Contact.h"
#import "myTableViewCell.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *contacts;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//初始化数据
self.contacts = [NSMutableArray arrayWithCapacity:];
for(int i=; i<; i++)
{
Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+] andFaceName:[NSString stringWithFormat:@"%d.png",i]];
[self.contacts addObject:conatct];
} //设置tableView的数据源
self.tableView.dataSource = self;
} #pragma mark -tableView的数据源方法
//每一组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contacts.count;
} //直接从xib文件中加载 //设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"myCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.如果没找到,就自己创建cell
if(!cell)
{
//从xib文件中加载视图
NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"MyCell" owner:nil options:nil];
cell = (UITableViewCell*)[views lastObject];
}
//3.设置单元格对象的内容
Contact *contact = [self.contacts objectAtIndex:indexPath.row];
UILabel *label = (UILabel*)[cell viewWithTag:];
label.text = contact.name;
UIImageView *imgView = (UIImageView*)[cell viewWithTag:];
[imgView setImage:[UIImage imageNamed:contact.faceName]]; return cell;
}

  

  方法四:在xib文件中创建单元格,并创建与之关联的的类,然后将加载过程封装到它的类中帮助初始化完成,同时该类提供类方法,最后再视图控制器中通过这个类方法获取单元格。

  首先在storyBoard中添加一个UITableView

  然后在已经创建好的MyCell.xib中创建自定义的单元格为:

  给单元格设置重用标识符identifier

  将单元格与自定义的类关联

  创建一个联系人初始化的类: 

 #import <Foundation/Foundation.h>

  @interface Contact : NSObject
@property (copy,nonatomic)NSString *name;
@property (copy,nonatomic)NSString *faceName;
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName;
@end #import "Contact.h" @implementation Contact
-(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName
{
self = [super init];
if(self)
{
_name = [name copy];
_faceName = [faceName copy];
}
return self;
}
@end

  创建一个与单元格关联的类:(将加载单元格的过程和属性封装起来)

  在视图控制器中通过上面的类方法获取单元格

 #import "ViewController.h"
#import "Contact.h"
#import "myTableViewCell.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *contacts;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//初始化数据
self.contacts = [NSMutableArray arrayWithCapacity:];
for(int i=; i<; i++)
{
Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+] andFaceName:[NSString stringWithFormat:@"%d.png",i]];
[self.contacts addObject:conatct];
} //设置tableView的数据源
self.tableView.dataSource = self;
} #pragma mark -tableView的数据源方法
//每一组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contacts.count;
}
//在与xib关联的类中加载xib文件(其实就是封装了一下而已) //设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"myCell";
myTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.如果没找到,就自己创建cell
if(!cell)
{
cell = [myTableViewCell cell];//调用类方法
}
//3.设置单元格对象的内容
Contact *contact = [self.contacts objectAtIndex:indexPath.row];
[cell setContact:contact];//调用实例方法 return cell;
} @end

iOS:UITableViewCell自定义单元格的更多相关文章

  1. Swift - 自定义单元格实现微信聊天界面

    1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示 ...

  2. 浅谈DevExpress<五>:TreeList简单的美化——自定义单元格,加注释以及行序号

    今天就以昨天的列表为例,实现以下效果:预算大于110万的单元格突出显示,加上行序号以及注释,如下图:

  3. jQuery MiniUI自定义单元格

    监听处理"drawcell"事件 使用"drawcell"事件,可以自定义单元格内容.样式.行样式等. grid.on("drawcell" ...

  4. 使用VUE组件创建SpreadJS自定义单元格(一)

    作为近五年都冲在热门框架排行榜首的Vue,大家一定会学到的一部分就是组件的使用.前端开发的模块化,可以让代码逻辑更加简单清晰,项目的扩展性大大加强.对于Vue而言,模块化的体现集中在组件之上,以组件为 ...

  5. 使用VUE组件创建SpreadJS自定义单元格(二)

    在上篇中,我们介绍了如何通过设置runtimeCompiler为true,在Vue中实现了动态创建电子表格组件.想了解具体内容可看点击查看使用VUE组件创建SpreadJS自定义单元格(一). 但是在 ...

  6. 自己的自定义单元格(IOS)

    定义自己的单位格有三种方法 - 代码 - xib - storyboard(推荐) 操作方法故事板 1.在TableView财产Prototype Cells至1.莫感觉1: 2.须要创建自己定义的单 ...

  7. IOS 取消表格单元格 TableViewCell 去掉高亮状态 点击Cell取消选择状态

    以下是两种实现效果 1. 自定义cell 继承UITableViewCell 重写 -(void)setSelected:(BOOL)selected animated:(BOOL)animated ...

  8. UITableView自定义单元格

    随手笔记: RootViewController代码 #import "RootViewController.h" #import "AddressContact.h&q ...

  9. NPOI 自定义单元格背景颜色-Excel

    NPOI针对office2003使用HSSFWorkbook,对于offce2007及以上使用XSSFWorkbook:今天我以HSSFWorkbook自定义颜色为例说明,Office2007的未研究 ...

随机推荐

  1. logstash部署及基本语法(二)

    一.logstash介绍 Logstash是一个开源的数据收集引擎,可以水平伸缩,而且logstash是整个ELK当中拥有最多插件的一个组件,其可以接收来自不同源的数据并统一输入到指定的且可以是不同目 ...

  2. python和redis简单交互

    python和redis简单交互 1.安装redis模块 pip3 install redis 2.redis模块简单使用: # /usr/bin/env python3 import redis c ...

  3. thinkphp中常用的模板变量

    在thinkphp中的模板要加载静态文件如css,js等文件时要经常用到模板常量. 假如项目放在/web/shop中,则如下所示对应常量的输出值: 1 2 3 4 5 6 7 8 9 // 不含域名 ...

  4. 网络请求+Gson解析--Retrofit 2

    其实内部是封装了Okhttp和Gson解析 public class CourseFragmentAPI { public static void get(String userId, BaseCal ...

  5. Java反射机制demo(三)—获取类中的构造函数

    Java反射机制demo(三)—获取类中的构造函数 1,获取类中所有的构造函数 如下面的代码中所示,这个类中显式的构造函数有五个. 空构造: public UserInfo() 带参构造有四个: pu ...

  6. windows svn 客户端连不上linux svn server

    采坑记录:linux服务器上svn://127.0.0.1可以正常使用,windows客户端远程连接不上,说明是端口号的问题. linux正常配置了iptables开启了3690端口,连接不上. 干脆 ...

  7. 关于zip伪加密

    创建一个zip文件 然后用winhex打开 可以看到第二个PK头的地方对应hex区域有一场串0000000000 在这里的第四个0这里末尾修改成奇数 奇数为加密 偶数为不加密 再去打开就可以看到加密了

  8. JVM内存模型以及垃圾回收

    JAVA堆的描述如下: 内存由Perm和Heap组成.其中Heap = {Old + NEW = { Eden , from, to } } JVM内存模型中分两大块: NEW Generation: ...

  9. 聊聊用CSS3来玩立方体

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 虽然现在很多浏览器都还不支持css3的3D转换,不过估计也已经有很多人都有玩css3的3D了......所以我这篇也就相当于水一下了,哈哈 ...

  10. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...