从nib文件加载自定义的UITableviewCell
2014-04-04 15:56
501 查看
[csharp] view
plaincopy
也许我们很多人都清楚怎么定制属于我们自己的UItableviewcell, 习惯了代码的我,竟然今天才彻底的理解了从nib文件加载自己定制的cell。
[javascript] view
plaincopy
registerNib:forCellReuseIdentifier:
Registers a nib object containing a cell with the table view under a specified identifier.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
Parameters
nib
A nib object that specifies the nib file to use to create the cell. This parameter cannot be nil.
identifier
The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.
Discussion
Prior to dequeueing any cells, call this method or the registerClass:forCellReuseIden
4000
tifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.
If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.
Availability
Available in iOS 5.0 and later.
See Also
tableView:cellForRowAtIndexPath: (UITableViewDataSource)
Declared In
UITableView.h
从官网的文档可以看出,通过 registerNib:forCellReuseIdentifier 这个方法可以实现。在使用复用机制之前要先确保 registernib这个方法,这个方法就是告诉了table是通过nib文件来创建cell的,这点很重要。
下面贴上主要的代码,这个是.h文件
[csharp] view
plaincopy
//
// ViewController.h
// TestCell
//
// Created by Juncy_Fan on 13-3-11.
// Copyright (c) 2013年 Juncy_Fan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
UITableView *table;
UINib *nib;
}
@end
下面的是.m文件
[csharp] view
plaincopy
//
// ViewController.m
// TestCell
//
// Created by Juncy_Fan on 13-3-11.
// Copyright (c) 2013年 Juncy_Fan. All rights reserved.
//
#import "ViewController.h"
#import "MyCell.h"
@interface ViewController ()
@end
@implementation ViewController
static BOOL nibsRegistered;
- (void)viewDidLoad
{
[super viewDidLoad];
nibsRegistered = NO;
NSLog(@"初始化:%d",nibsRegistered);
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:table];
table.delegate = self;
table.dataSource = self;
[table release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 300;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identy = @"CustomCell";
if (!nib) {
nib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:identy];
NSLog(@"我是从nib过来的,%d",indexPath.row);
}
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
NSUInteger row = [indexPath row];
if (row%2 == 0) {
cell.myTitle.text = @"我是偶数行的";
cell.mySubTitle.text = @"我是子标题";
cell.imageicon.image = [UIImage imageNamed:@"201202.png"];
}else{
cell.myTitle.text = @"我是奇数行的";
cell.mySubTitle.text = @"我是奇数行的子标题";
cell.imageicon.image = [UIImage imageNamed:@"201203.png"];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
@end
ok啦,定制cell的时候,nib文件里面identifer一定要和代码里面的保持一致哦。
plaincopy
也许我们很多人都清楚怎么定制属于我们自己的UItableviewcell, 习惯了代码的我,竟然今天才彻底的理解了从nib文件加载自己定制的cell。
[javascript] view
plaincopy
registerNib:forCellReuseIdentifier:
Registers a nib object containing a cell with the table view under a specified identifier.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
Parameters
nib
A nib object that specifies the nib file to use to create the cell. This parameter cannot be nil.
identifier
The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.
Discussion
Prior to dequeueing any cells, call this method or the registerClass:forCellReuseIden
4000
tifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.
If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.
Availability
Available in iOS 5.0 and later.
See Also
tableView:cellForRowAtIndexPath: (UITableViewDataSource)
Declared In
UITableView.h
从官网的文档可以看出,通过 registerNib:forCellReuseIdentifier 这个方法可以实现。在使用复用机制之前要先确保 registernib这个方法,这个方法就是告诉了table是通过nib文件来创建cell的,这点很重要。
下面贴上主要的代码,这个是.h文件
[csharp] view
plaincopy
//
// ViewController.h
// TestCell
//
// Created by Juncy_Fan on 13-3-11.
// Copyright (c) 2013年 Juncy_Fan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
UITableView *table;
UINib *nib;
}
@end
下面的是.m文件
[csharp] view
plaincopy
//
// ViewController.m
// TestCell
//
// Created by Juncy_Fan on 13-3-11.
// Copyright (c) 2013年 Juncy_Fan. All rights reserved.
//
#import "ViewController.h"
#import "MyCell.h"
@interface ViewController ()
@end
@implementation ViewController
static BOOL nibsRegistered;
- (void)viewDidLoad
{
[super viewDidLoad];
nibsRegistered = NO;
NSLog(@"初始化:%d",nibsRegistered);
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:table];
table.delegate = self;
table.dataSource = self;
[table release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 300;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identy = @"CustomCell";
if (!nib) {
nib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:identy];
NSLog(@"我是从nib过来的,%d",indexPath.row);
}
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
NSUInteger row = [indexPath row];
if (row%2 == 0) {
cell.myTitle.text = @"我是偶数行的";
cell.mySubTitle.text = @"我是子标题";
cell.imageicon.image = [UIImage imageNamed:@"201202.png"];
}else{
cell.myTitle.text = @"我是奇数行的";
cell.mySubTitle.text = @"我是奇数行的子标题";
cell.imageicon.image = [UIImage imageNamed:@"201203.png"];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
@end
ok啦,定制cell的时候,nib文件里面identifer一定要和代码里面的保持一致哦。
相关文章推荐
- 从nib文件加载自定义的UITableviewCell
- log4j加载自定义的日志的配置文件
- ASP.NET MVC5重写RazorViewEngine,能根据地址自定义加载视图页面文件
- 2.0Tip/Trick之加载XAP, 加载XAML, 加载DLL, AppManifest.xaml文件说明, 自定义鼠标指针
- 自定义文件系统类加载器
- Android中layout.xml文件中加载自定义的View类
- iOS中读取Cell的两种方式(资源包种加载Cell,从nib文件中注册cell)和xib知识点
- 通过Dalvik加载自定义类(而不是从默认的dex文件里面去加载)
- JVM 核心机制(类加载器、自定义文件系统类加载器、网络自定义类加载器
- 自定义配置文件的使用及加载-txt
- 自定义classload加载自定义的class文件
- 加载nib中自定义UIView
- 自定义js模板加载数据文件
- 【java】自定义ClassLoader 加载外部类和jar文件
- 从nib文件加载视图的方法
- 一种自定义js文件加载时间的方法
- nib 文件加载
- Vue填坑之旅1-加载自定义字体文件
- 基于Python OpenCV加载视频文件,显示,并绘制自定义文字
- 如何加载和使用配置文件中的自定义实体属性