前言:#####
對(duì)于搞IOS開發(fā)的只要提起UITableView這個(gè)控件,我想沒人會(huì)說他不了解吧,以為這個(gè)控件太基本太核心太好用了,因此我們必須熟練掌握它的使用,以下文章中我會(huì)從最基本的UITableView的使用直到UITableView數(shù)據(jù)源的分離,講述我對(duì)UITableView的理解,廢話不多說開擼。
流程:#####
- 純代碼實(shí)現(xiàn)一個(gè)UITableView
- TableViewCell 重復(fù)利用
- UITableViewCell的多種創(chuàng)建方式
純代碼實(shí)現(xiàn)一個(gè)UITableView#####
#import "ViewController.h"
// 使用tableView UITableViewDataSource 必須實(shí)現(xiàn),有了他tableView對(duì)象才能拿到自己想要的數(shù)據(jù)
@interface ViewController () <UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
/**
* 通過懶加載方式創(chuàng)建tableView對(duì)象
*
* @return tableView對(duì)象
*/
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
CGSize SCREEN_SIZE = [UIScreen mainScreen].bounds.size;
CGFloat tableViewX = 0;
CGFloat tableViewY = 64;
CGFloat tableViewWidth = SCREEN_SIZE.width;
CGFloat tableViewHeight = SCREEN_SIZE.height-tableViewY;
CGRect tableRect = CGRectMake(tableViewX, tableViewY, tableViewWidth, tableViewHeight);
_tableView.frame = tableRect;
_tableView.dataSource = self;
}
return _tableView;
}
// tableView 的代理,用來告訴tableView共有多少條數(shù)據(jù)Cell(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
// tableView 的代理,用來告訴tableView每個(gè)cell都長(zhǎng)什么樣子(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]init];
cell.textLabel.text = @"tableView測(cè)試";
return cell;
}
效果圖:
TableViewCell 重復(fù)利用:
- 分析:
雖然上面的代碼實(shí)現(xiàn)了使用tableView展示數(shù)據(jù),但是性能很差,為什么這么說,細(xì)心的人可能會(huì)有這樣的疑問,UITableViewCell好像無止盡的在創(chuàng)建,少量的還能接受,如果有千萬(wàn)條數(shù)據(jù),那么對(duì)于內(nèi)存毫無疑問是一個(gè)打擊,勢(shì)必影響用戶的體驗(yàn),這里我們開始解釋一個(gè)新的名詞緩存池,他完美的解決了這個(gè)問題,有了緩存池之后系統(tǒng)只會(huì)創(chuàng)建屏幕范圍內(nèi)所包含的TableViewCell數(shù)量,當(dāng)用戶滑動(dòng)屏幕時(shí),被隱藏的Cell對(duì)象,系統(tǒng)會(huì)將該Cell扔到緩存池當(dāng)中,等到下次被使用,而這個(gè)被隱藏的Cell也就是即將顯示出來的新的Cell,Cell對(duì)象其實(shí)沒發(fā)生變化的,變化的只是Cell內(nèi)部的數(shù)據(jù),有點(diǎn)抽象舉個(gè)簡(jiǎn)單的例子,用五個(gè)袋子裝蘋果,目的:將蘋果從地里搬運(yùn)到地的頂頭,我們要做的就是在地里把蘋果塞滿袋子,搬運(yùn)到地的頂頭,倒出蘋果再拿著袋子去地里裝蘋果,這樣我們就可以省掉好多人工成本,重復(fù)利用這個(gè)五個(gè)袋子。
- 代碼:
#import "ViewController.h"
// 使用tableView UITableViewDataSource 必須實(shí)現(xiàn)
@interface ViewController () <UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
/**
* 通過懶加載方式創(chuàng)建tableView對(duì)象
*
* @return tableView對(duì)象
*/
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
CGSize SCREEN_SIZE = [UIScreen mainScreen].bounds.size;
CGFloat tableViewX = 0;
CGFloat tableViewY = 64;
CGFloat tableViewWidth = SCREEN_SIZE.width;
CGFloat tableViewHeight = SCREEN_SIZE.height-tableViewY;
CGRect tableRect = CGRectMake(tableViewX, tableViewY, tableViewWidth, tableViewHeight);
_tableView.frame = tableRect;
_tableView.dataSource = self;
}
return _tableView;
}
// tableView 的代理,用來告訴tableView共有多少條數(shù)據(jù)Cell(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
// tableView 的代理,用來告訴tableView每個(gè)cell都長(zhǎng)什么樣子(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 緩存池里可能存在很多個(gè)tableViewCell對(duì)象,為了區(qū)分它們,我們必須創(chuàng)建一個(gè)唯一標(biāo)識(shí)
static NSString *identifier = @"cell";
// 檢查緩存池是否有對(duì)應(yīng)標(biāo)識(shí)的cell,如果有直接拿出來使用,如果沒有創(chuàng)建并且扔到將新創(chuàng)建的cell扔到緩存池
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]init];
}
cell.textLabel.text = @"tableView 測(cè)試";
return cell;
}
- 驗(yàn)證:
為了驗(yàn)證我的說法,我們現(xiàn)在創(chuàng)建10條cell,然后打印cell對(duì)象,根據(jù)他們的內(nèi)存地址判斷他們是否被重復(fù)利用。
#import "ViewController.h"
// 使用tableView UITableViewDataSource 必須實(shí)現(xiàn)
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
/**
* 通過懶加載方式創(chuàng)建tableView對(duì)象
*
* @return tableView對(duì)象
*/
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
CGSize SCREEN_SIZE = [UIScreen mainScreen].bounds.size;
CGFloat tableViewX = 0;
CGFloat tableViewY = 64;
CGFloat tableViewWidth = SCREEN_SIZE.width;
CGFloat tableViewHeight = SCREEN_SIZE.height-tableViewY;
CGRect tableRect = CGRectMake(tableViewX, tableViewY, tableViewWidth, tableViewHeight);
_tableView.frame = tableRect;
_tableView.dataSource = self;
_tableView.delegate = self;
}
return _tableView;
}
// tableView 的代理,用來告訴tableView共有多少條數(shù)據(jù)Cell(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
// tableView 的代理,用來告訴tableView每個(gè)cell都長(zhǎng)什么樣子(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 緩存池里可能存在很多個(gè)tableViewCell對(duì)象,為了區(qū)分它們,我們必須創(chuàng)建一個(gè)唯一標(biāo)識(shí)
static NSString *identifier = @"cell";
// 檢查緩存池是否有對(duì)應(yīng)標(biāo)識(shí)的cell,如果有直接拿出來使用,如果沒有創(chuàng)建并且扔到將新創(chuàng)建的cell扔到緩存池
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]init];
}
cell.textLabel.text = @"tableView 測(cè)試";
NSLog(@"Cell --> %@",cell);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
日志Log:
**2016-07-11 11:20:32.265 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c3e6f0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c3eb50>>**
**2016-07-11 11:20:32.267 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623f28170; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623f2c740>>**
**2016-07-11 11:20:32.268 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623d5ca10; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623d5a9c0>>**
**2016-07-11 11:20:32.268 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623ea4010; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623ea3070>>**
**2016-07-11 11:20:37.120 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c27ea0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c411f0>>**
**2016-07-11 11:20:38.301 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c3e6f0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c72740>>**
**2016-07-11 11:20:39.997 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c1bbd0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c43160>>**
**2016-07-11 11:20:40.377 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c1eee0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c4e710>>**
**2016-07-11 11:20:42.153 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c1f650; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c293f0>>**
**2016-07-11 11:20:42.562 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c27ea0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c3c1e0>>**
**2016-07-11 11:21:05.947 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c2a660; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c265f0>>**
**2016-07-11 11:21:06.390 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623d5ca10; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623d59b10>>**
**2016-07-11 11:21:07.704 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623d17df0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623d5d460>>**
**2016-07-11 11:21:08.163 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c1eee0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c28d40>>**
**2016-07-11 11:21:10.269 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c20cc0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c73040>>**
**2016-07-11 11:21:10.753 UITableViewDemo[1862:66650] Cell --> <UITableViewCell: 0x7fb623c27ea0; frame = (0 0; 320 44); text = 'tableView ****測(cè)試****'; layer = <CALayer: 0x7fb623c3dde0>>**
- 結(jié)論:通過日志我們可以發(fā)現(xiàn)有好多Cell內(nèi)存地址一樣,那只能證明我的說法是正確的,緩存池確實(shí)讓tableViewCell達(dá)到了重復(fù)利用的。
UITableViewCell的多種創(chuàng)建模式:
UITableViewCell一般可以使用系統(tǒng)自帶的(UITableViewCell),也可以繼承UITableViewCell類創(chuàng)建自定義的Cell,自定義的Cell的創(chuàng)建一般有兩種方式,一種是通過純代碼創(chuàng)建,一種是使用xib方式創(chuàng)建,這些方式?jīng)]有好與壞之分,只能根據(jù)實(shí)際需求選擇最適合自己的方式就行,下面我們快速的通過這幾種方式創(chuàng)建tableViewCell。
- .系統(tǒng)默認(rèn)的TableViewCell:
#import "ViewController.h"
// 使用tableView UITableViewDataSource 必須實(shí)現(xiàn)
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
/**
* 通過懶加載方式創(chuàng)建tableView對(duì)象
*
* @return tableView對(duì)象
*/
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
CGSize SCREEN_SIZE = [UIScreen mainScreen].bounds.size;
CGFloat tableViewX = 0;
CGFloat tableViewY = 64;
CGFloat tableViewWidth = SCREEN_SIZE.width;
CGFloat tableViewHeight = SCREEN_SIZE.height-tableViewY;
CGRect tableRect = CGRectMake(tableViewX, tableViewY, tableViewWidth, tableViewHeight);
_tableView.frame = tableRect;
_tableView.dataSource = self;
_tableView.delegate = self;
}
return _tableView;
}
// tableView 的代理,用來告訴tableView共有多少條數(shù)據(jù)Cell(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
// tableView 的代理,用來告訴tableView每個(gè)cell都長(zhǎng)什么樣子(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 緩存池里可能存在很多個(gè)tableViewCell對(duì)象,為了區(qū)分它們,我們必須創(chuàng)建一個(gè)唯一標(biāo)識(shí)
static NSString *identifier = @"cell";
// 檢查緩存池是否有對(duì)應(yīng)標(biāo)識(shí)的cell,如果有直接拿出來使用,如果沒有創(chuàng)建并且扔到將新創(chuàng)建的cell扔到緩存池
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
// 系統(tǒng)默認(rèn)提供的cell的樣式有4種
// UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
// UITableViewCellStyleValue1, // 設(shè)置cell的詳細(xì)內(nèi)容顯示在主文字的后面
// UITableViewCellStyleValue2, // 設(shè)置cell的詳細(xì)內(nèi)容顯示在主文字的前面(會(huì)覆蓋前面imageView)
// UITableViewCellStyleSubtitle // 設(shè)置cell的詳細(xì)內(nèi)容顯示在主文字的下方
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// 設(shè)置主文字
cell.textLabel.text = @"tableView 測(cè)試";
// 設(shè)置圖片
cell.imageView.image = [UIImage imageNamed:@"icon"];
// 設(shè)置cell的詳細(xì)內(nèi)容 并且cell的樣式不能填寫 (UITableViewCellStyleDefault),如果填寫,則cell的詳細(xì)內(nèi)容不顯示
cell.detailTextLabel.text = @"tableViewCell的詳細(xì)內(nèi)容";
// 設(shè)置cell后端顯示的圖標(biāo)(使用系統(tǒng)提供的圖標(biāo))
// UITableViewCellAccessoryNone, 什么都不顯示
// UITableViewCellAccessoryDisclosureIndicator, 顯示一個(gè)指向右端的箭頭
// UITableViewCellAccessoryDetailDisclosureButton __TVOS_PROHIBITED, 顯示一個(gè)嘆號(hào)
// UITableViewCellAccessoryCheckmark, 顯示一個(gè)選中的對(duì)號(hào)
// UITableViewCellAccessoryDetailButton 顯示一個(gè)嘆號(hào)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 自定義cell末尾顯示的UIView對(duì)象
cell.accessoryView = [[UISwitch alloc]init];
return cell;
}
- 自定義TableViewCell:
效果:
在自定義的Cell中添加兩個(gè)圖片和一個(gè)TableView視圖。
// 自定義Cell部分:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface TempTableView : NSObject <UITableViewDataSource,UITableViewDelegate>
@end
@interface CustomTableViewCell : UITableViewCell
@end
#import "CustomTableViewCell.h"
@interface CustomTableViewCell()
@property (nonatomic, strong) UIImageView *image1;
@property (nonatomic, strong) UIImageView *image2;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TempTableView *tempTableVew;
@end
@implementation TempTableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]init];
}
cell.textLabel.text = @"測(cè)試數(shù)據(jù)";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
@end
@implementation CustomTableViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self addSubViews];
}
return self;
}
- (UIImageView *)image1
{
if (!_image1) {
_image1 = [[UIImageView alloc]init];
_image1.image = [UIImage imageNamed:@"icon1"];
}
return _image1;
}
- (UIImageView *)image2
{
if (!_image2) {
_image2 = [[UIImageView alloc]init];
_image2.image = [UIImage imageNamed:@"icon"];
}
return _image2;
}
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
_tableView.dataSource = self.tempTableVew;
_tableView.delegate = self.tempTableVew;
}
return _tableView;
}
- (TempTableView *)tempTableVew
{
if (!_tempTableVew) {
_tempTableVew = [[TempTableView alloc]init];
}
return _tempTableVew;
}
- (void)addSubViews
{
[self addSubview:self.image1];
[self addSubview:self.image2];
[self addSubview:self.tableView];
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect rect = self.frame;
rect.size.width = self.frame.size.width / 2 ;
rect.origin.y = 0;
rect.origin.x = rect.size.width;
self.tableView.frame = rect;
CGRect rectI1 = self.frame;
rectI1.origin.y = 0;
rectI1.size.width = self.frame.size.width / 4 ;
self.image1.frame = rectI1;
CGRect rectI2 = self.frame;
rectI2.origin.y = 0;
rectI2.size.width = self.frame.size.width / 4 ;
rectI2.origin.x = rectI2.size.width;
self.image2.frame = rectI2;
}
@end
// 展示部分
#import "ViewController.h"
#import "CustomTableViewCell.h"
// 使用tableView UITableViewDataSource 必須實(shí)現(xiàn)
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
/**
* 通過懶加載方式創(chuàng)建tableView對(duì)象
*
* @return tableView對(duì)象
*/
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
CGSize SCREEN_SIZE = [UIScreen mainScreen].bounds.size;
CGFloat tableViewX = 0;
CGFloat tableViewY = 64;
CGFloat tableViewWidth = SCREEN_SIZE.width;
CGFloat tableViewHeight = SCREEN_SIZE.height-tableViewY;
CGRect tableRect = CGRectMake(tableViewX, tableViewY, tableViewWidth, tableViewHeight);
_tableView.frame = tableRect;
_tableView.dataSource = self;
_tableView.delegate = self;
}
return _tableView;
}
// tableView 的代理,用來告訴tableView共有多少條數(shù)據(jù)Cell(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
// tableView 的代理,用來告訴tableView每個(gè)cell都長(zhǎng)什么樣子(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 緩存池里可能存在很多個(gè)tableViewCell對(duì)象,為了區(qū)分它們,我們必須創(chuàng)建一個(gè)唯一標(biāo)識(shí)
static NSString *identifier = @"cell";
// 檢查緩存池是否有對(duì)應(yīng)標(biāo)識(shí)的cell,如果有直接拿出來使用,如果沒有創(chuàng)建并且扔到將新創(chuàng)建的cell扔到緩存池
CustomTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[CustomTableViewCell alloc]init];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
@end
效果圖:
- 通過xib方式實(shí)現(xiàn)TableViewCell:
xib 方式的使用步驟:
- 創(chuàng)建一個(gè)繼承UITabelViewCell的類文件。
- 在創(chuàng)建UITabelViewCell類文件時(shí),系統(tǒng)未能幫我們創(chuàng)建綁定的xib文件,那么我們必須手動(dòng)創(chuàng)建,xib文件名稱保持和UITabelViewCell類文件的名稱一致,帶創(chuàng)建好xib之后,刪除xib中的UIView對(duì)象,將UITableViewCell控件拖入到xib中。
-
配置xib文件
QQ20160711-0.png
首先選中xib中UITableViewCell控件,看上圖紅色區(qū)域,Class選項(xiàng)選擇剛創(chuàng)建的UITabelViewCell類文件,下面的Restoration ID 就是那個(gè)緩存用的唯一標(biāo)識(shí)。
#import <UIKit/UIKit.h>
@interface XibTableViewCell : UITableViewCell
@property (nonatomic, copy) NSString *title;
@end
#import "XibTableViewCell.h"
@interface XibTableViewCell()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation XibTableViewCell
- (void)setTitle:(NSString *)title
{
_title = title;
self.label.text = _title;
}
@end
#import "ViewController.h"
#import "CustomTableViewCell.h"
#import "XibTableViewCell.h"
// 使用tableView UITableViewDataSource 必須實(shí)現(xiàn)
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
/**
* 通過懶加載方式創(chuàng)建tableView對(duì)象
*
* @return tableView對(duì)象
*/
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
CGSize SCREEN_SIZE = [UIScreen mainScreen].bounds.size;
CGFloat tableViewX = 0;
CGFloat tableViewY = 64;
CGFloat tableViewWidth = SCREEN_SIZE.width;
CGFloat tableViewHeight = SCREEN_SIZE.height-tableViewY;
CGRect tableRect = CGRectMake(tableViewX, tableViewY, tableViewWidth, tableViewHeight);
_tableView.frame = tableRect;
_tableView.dataSource = self;
_tableView.delegate = self;
}
return _tableView;
}
// tableView 的代理,用來告訴tableView共有多少條數(shù)據(jù)Cell(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
// tableView 的代理,用來告訴tableView每個(gè)cell都長(zhǎng)什么樣子(這個(gè)代理是強(qiáng)制性的,必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
XibTableViewCell *cell = [[NSBundle mainBundle]loadNibNamed:@"XibTableViewCell" owner:nil options:nil].firstObject;
cell.title = [NSString stringWithFormat:@"%@",@(indexPath.row)];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
@end
效果圖:
效果圖
總結(jié):
以上就是三種創(chuàng)建UITableViewCell的方式,自定義適合強(qiáng)自定義的界面設(shè)計(jì),xib適合使用使用系統(tǒng)提供的控件對(duì)象界面設(shè)計(jì),UITableViewCell適合簡(jiǎn)單的界面設(shè)計(jì)。