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



#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的上面



#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自定义单元格的更多相关文章
- Swift - 自定义单元格实现微信聊天界面
1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示 ...
- 浅谈DevExpress<五>:TreeList简单的美化——自定义单元格,加注释以及行序号
今天就以昨天的列表为例,实现以下效果:预算大于110万的单元格突出显示,加上行序号以及注释,如下图:
- jQuery MiniUI自定义单元格
监听处理"drawcell"事件 使用"drawcell"事件,可以自定义单元格内容.样式.行样式等. grid.on("drawcell" ...
- 使用VUE组件创建SpreadJS自定义单元格(一)
作为近五年都冲在热门框架排行榜首的Vue,大家一定会学到的一部分就是组件的使用.前端开发的模块化,可以让代码逻辑更加简单清晰,项目的扩展性大大加强.对于Vue而言,模块化的体现集中在组件之上,以组件为 ...
- 使用VUE组件创建SpreadJS自定义单元格(二)
在上篇中,我们介绍了如何通过设置runtimeCompiler为true,在Vue中实现了动态创建电子表格组件.想了解具体内容可看点击查看使用VUE组件创建SpreadJS自定义单元格(一). 但是在 ...
- 自己的自定义单元格(IOS)
定义自己的单位格有三种方法 - 代码 - xib - storyboard(推荐) 操作方法故事板 1.在TableView财产Prototype Cells至1.莫感觉1: 2.须要创建自己定义的单 ...
- IOS 取消表格单元格 TableViewCell 去掉高亮状态 点击Cell取消选择状态
以下是两种实现效果 1. 自定义cell 继承UITableViewCell 重写 -(void)setSelected:(BOOL)selected animated:(BOOL)animated ...
- UITableView自定义单元格
随手笔记: RootViewController代码 #import "RootViewController.h" #import "AddressContact.h&q ...
- NPOI 自定义单元格背景颜色-Excel
NPOI针对office2003使用HSSFWorkbook,对于offce2007及以上使用XSSFWorkbook:今天我以HSSFWorkbook自定义颜色为例说明,Office2007的未研究 ...
随机推荐
- 第一个web程序(ServletRequest , ServletResponse)
一.ServletRequest 1.获取参数的方法(四种) > String getParameter(String name): 根据请求参数的名字, 返回参数值. 若请求参数有多个值(例如 ...
- 如何让 C++ 和 C# 一样易用,而且效率更高?
(未完成,待续..) 第一章:C++基本功 1. 养成良好的编码习惯. 2. 堆上的资源随用随还,严格控制生命周期. 第二章:一些调试心得 1. 在VisualStudio环境下,曾出现 Detect ...
- Python实现代码行数统计工具
我们经常想要统计项目的代码行数,但是如果想统计功能比较完善可能就不是那么简单了, 今天我们来看一下如何用python来实现一个代码行统计工具. 思路:首先获取所有文件,然后统计每个文件中代码的行数,最 ...
- ref:CodeIgniter框架内核设计缺陷可能导致任意代码执行
ref:https://www.seebug.org/vuldb/ssvid-96217 简要描述: 为准备乌云深圳沙龙,准备几个0day做案例. 官方承认这个问题,说明会发布补丁,但不愿承认这是个『 ...
- Entity Framework Core(3)-配置DbContext
设计时 DbContext 配置 EF Core 设计时工具如迁移需要能够发现和创建的工作实例DbContext以收集有关应用程序的实体类型以及它们如何映射到数据库架构的详细信息的类型. 此过程可以为 ...
- python 列表的浅拷贝和深拷贝
转自:https://www.cnblogs.com/laolibk/p/7821369.html 浅拷贝 shallow copy 和深拷贝 deep copy list.copy() 浅拷贝:复制 ...
- BeautifulSoup与Xpath解析库总结
一.BeautifulSoup解析库 1.快速开始 html_doc = """ <html><head><title>The Dor ...
- redis修改密码
## 无需添加密码参数 redis-cli.exe -h 127.0.0.1 -p 6379 ## 获取当前密码 config get requirepass ## 设置当前密码,服务重新启动后又会置 ...
- 模型构建<2>:不平衡样本集的处理
分类预测建模都有一个基本的假设,即样本集中不同类别的样本个数基本相同,但是在实际任务中,经常会出现各类样本个数差别较大的情况,这样的样本集就是不平衡样本集,它对学习建模的性能会带来很大的影响,因此必须 ...
- [CC-SUBWAY]Subway Ride
[CC-SUBWAY]Subway Ride 题目大意: 一棵\(n(n\le5\times10^5)\)个点的含重边的树,总边数为\(m(m\le10^6)\),每条边有一个颜色.\(q(q\le5 ...