ios 怎么ios xib 注册cell加载xib cell好

40121人阅读
我们以前通常会这样做
- (UITableViewCell& *)tableView:(UITableView&*)tableView
cellForRowAtIndexPath:(NSIndexPath&*)indexPath{
& & &static&&NSString&
*CellIdentiferId =&@&MomentsViewControllerCellID&;
& & &MomentsCell& *cell = [tableView&dequeueReusableCellWithIdentifier:CellIdentiferId];
& & &if&(cell ==&nil)
&&&&&&&&NSArray&*nibs = [[NSBundle&mainBundle]loadNibNamed:@&MomentsCell&&owner:nil&options:nil];
&&&&&&& cell = [nibs&lastObject];
&&&&&&& cell.backgroundColor&= [UIColor&clearColor];
& & &return&
严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的,所以,需要修改啊,一种修改方式是使用如下ios5提供的新方式:
- (void)registerNib:(UINib&*)nib
forCellReuseIdentifier:(NSString&*)identifier
还有就是在xib页面设置好(ios5之前也可以用的)
如果忘了在xib中设置,还有一种方式 &
NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@&CustomTableCell& owner:nil options:nil];
for (id obj in nibObjects)
if ([obj isKindOfClass:[CustomTableCell class]])
&&&&&&& cell = obj;
[cell setValue:cellId forKey:@&reuseIdentifier&];
还有更常用的
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return[self.items count];}&-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@&myCell&];
if(!cell){
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@&myCell&];}
cell.textLabel.text =[self.items objectAtIndex:indexPath.row];
return cell;
但是现在有一种新的方式了,可以采用如下的方式
采用registerNib的方式,并且把设置都放在了willDisplayCell方法中了,而不是以前我们经常用的cellForRowAtIndexPath
这个方法我试了下,如果我们在xib中设置了 Identifier,那么此处的必须一致,否则会crash的
- (UITableViewCell&*)tableView:(UITableView&*)tableView
cellForRowAtIndexPath:(NSIndexPath&*)indexPath
&&& MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@&myCell&];
&&&&if&(!cell)
&&&&&&& [tableView registerNib:[UINib nibWithNibName:@&MyCustomCell&&bundle:nil]
forCellReuseIdentifier:@&myCell&];
&&&&&&& cell = [tableView dequeueReusableCellWithIdentifier:@&myCell&];
&&&&return&
- (void)tableView:(UITableView&*)tableView
willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:(NSIndexPath&*)indexPath
&&& cell.leftLabel.text = [self.items objectAtIndex:indexPath.row];
&&& cell.rightLabel.text = [self.items objectAtIndex:indexPath.row];
&&& cell.middleLabel.text = [self.items objectAtIndex:indexPath.row];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:233978次
积分:2447
积分:2447
排名:第10717名
原创:40篇
转载:13篇
评论:65条
(1)(2)(1)(1)(4)(1)(1)(1)(1)(3)(1)(1)(3)(2)(1)(1)(1)(4)(1)(1)(1)(6)(1)(2)(2)(1)(2)(1)(1)(2)(2)(2)App is crashing when loading custom xib file for UITableViewCell [应用程序崩溃时加载自定义为UITableViewCell xib文件] - 问题-字节技术
App is crashing when loading custom xib file for UITableViewCell
应用程序崩溃时加载自定义为UITableViewCell xib文件
问题 (Question)
When I try to load custom UITableViewCell app crashes. I've done this many times before and this time I did it the same way. Part of the code where which cause crash is commented (// This part is causing crash):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// This part is causing crash
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];
cell = [topLevelObjects objectAtIndex:0];
There is CustomTableCell.h, CustomTableCell.m and CustomTableCell.xib.
CellIdentifier is CustomTableCell and there is no space problem with nib name.
Screenshot of error:
What could be a reason for crash?
当我试图加载自定义UITableViewCell应用程序崩溃。我以前有过很多次这样的经历,这一次我做到了同样的方式。部分代码导致崩溃在哪里评论(/ /这部分导致崩溃):- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// This part is causing crash
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];
cell = [topLevelObjects objectAtIndex:0];
CustomTableCell。CustomTableCell h。米和CustomTableCell.xib。CellIdentifier CustomTableCell并没有nib名称空间问题。错误的截图:崩溃的原因可能是??么?
最佳答案 (Best Answer)
I created new xib file for custom cell and everything is ok now.
解决了。我创建了新xib文件自定义单元,现在一切都好。
答案 (Answer) 2
Try the following code :
#import "customCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// Configure the cell.
static NSString *CustomCellIdentifier = @"customCell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[customCell class]])
cell = (customCell *)oneO
cell.lbl.text = @"Hello";
Hopefully, it'll help you.
试试下面的代码:#import "customCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// Configure the cell.
static NSString *CustomCellIdentifier = @"customCell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[customCell class]])
cell = (customCell *)oneO
cell.lbl.text = @"Hello";
答案 (Answer) 3
Try this one..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
CustomTableCell *
cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableCell"];
for (UIControl *subview in cell.contentView.subviews) {
[subview removeFromSuperview];
if(cell == nil)
nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
for(id oneobject in nib)
if([oneobject isKindOfClass:[CustomTableCell class]])
cell = (CustomTableCell *)
hope it helps..
试试这个. .- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
CustomTableCell *
cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableCell"];
for (UIControl *subview in cell.contentView.subviews) {
[subview removeFromSuperview];
if(cell == nil)
nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
for(id oneobject in nib)
if([oneobject isKindOfClass:[CustomTableCell class]])
cell = (CustomTableCell *)
答案 (Answer) 4
The code looks ok so far. What could be the problem is, that you didn't link the xib to your customized class in the editor. And if so, you might have a problem, that some of the linked Outlets don't exist (or so, it depends on the errors).
You can link your customized table view cell in the editor under "Custom Class". Do so by clicking in the xib file editor on your custom cell (in the left section you find the Section "Objects" and below this, you should find your table view cell).
If you have the Attribute-Editor open, click on the thrid tab (so you see, where you can set your custom class. Enter the name of your class (in your case, its "CustomTableCell"). Click the right arrow right next to the entered class to verify, if your custom class exists. Recompile (optional make a clean) and it should work fine.
代码看起来好为止。可能的问题是什么,,你没有链接编辑器中的xib定制类。如果是这样,您可能有一个问题,,一些有关媒体不存在(左右,这取决于错误)。您可以联系您的自定义表视图单元格编辑器中的“自定义类”。这样做在xib文件编辑器中点击您的自定义细胞(在左边部分你找到“对象”,在这个部分,你应该找到你的表视图单元)。如果你有打开属性编辑器,单击穿过选项卡(所以你看,你可以设置你的自定义类。输入您的类的名称(在你的情况下,其“CustomTableCell”)。单击右箭头旁边输入的类来验证,如果您的自定义类的存在。重新编译(可选干净),它可正常工作。
答案 (Answer) 5
Did you check Custom Cell's connection instead of File's Owner in class. ?
你是否检查自定义单元的连接,而不是在课堂上文件的所有者。吗?
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:114网址导航iOS开发,怎么注册xib自定义的cell?_百度知道
iOS开发,怎么注册xib自定义的cell?
1、先把Cell的头文件import进来2、[tableview_main registerNib:[UINib nibWithNibName:@&UserCallDealTableViewCell& bundle:nil]
forCellReuseIdentifier:@&UserCallDealTableViewCellMark&];使用这个方法注册自定义Cell
tableview_main就是当前tableview实力化对象,然后UserCallDealTableViewCell这个字符串就是xib的名称,UserCallDealTableViewCellMark是重用机制的标记,配合等一下的代理方法使用3、最后在代理方法控制自定义cell-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *identification = @&UserCallDealTableViewCellMark&;
UserCallDealTableViewCell *cell = [tableview_main dequeueReusableCellWithIdentifier:identification];
if (cell != nil) {
NSDictionary *dict_tmp = arry_dataSource[indexPath.row];
cell-&label_doctor_name.text = dict_tmp[@&doctorName&];
cell-&label_hosptial.text = dict_tmp[@&hospitalName&];
if ([dict_tmp[@&sstatus&] isEqualToString:@&S&]) {
cell-&label_status.text = @&预约成功&;
cell-&label_status.text = @&预约失败&;
NSString *string_date = @{@&0&:@&上午&,@&1&:@&下午&}[dict_tmp[@&timeq&]];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@&yyyy-MM-dd&];
NSDate *date_tmp = [formatter dateFromString:dict_tmp[@&date&]];
[formatter setDateFormat:[NSString stringWithFormat:@&MM月dd日
EEEE %@ mm:HH&,string_date]];
cell-&label_info.text = [formatter stringFromDate:date_tmp];
return [UITableViewCell new];//这个地方我建议不要返回nil因为可能会导致崩溃}
其他类似问题
为您推荐:
提问者采纳
owner- (UITableViewCell *)tableV;UISpecialTableViewCell&quot:self options:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @&quot:CellIdentifier]:@& 自己的一些设置
return (UITableViewCell *)
if (cell == nil)
cell= (UITwitterTableViewCell *)[[[NSBundle
mainBundle]
loadNibNUISpecialTableViewCell&
UISpecialTableViewCell *cell = (UISpecialTableViewCell*)[tableView dequeueReusableCellWithIdentifier:nil]
lastObject]:(UITableView *)tableView cellForRowAtIndexPath
提问者评价
其他3条回答
forCellReuseI以上代码是用来注册的:@&];nibWithNibName.tableView&nbsp:[UINib&registerNCustomCellIdentifier&quot:nil]&nbsp:@&[&nbsp&&CustomCell&quot
实现awakeFromNib方法加载cell
不太明白你的意思,你能详细的描述下么
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁iOS自定义TableViewCell详解[两种步骤]
iOS自定义TableViewCell详解[两种步骤]
&iOS自定义TableViewCell详解[两种方法]今天要跟大家分享的是两种自定义UITableViewCell方法。一、首先看看效
iOS自定义TableViewCell详解[两种方法]今天要跟大家分享的是两种自定义UITableViewCell方法。一、首先看看效果1)第一种是通过nib文件加载的方式,在UITableView里面添加自定义的Cell。2)第二种是代码里面自定义Cell的形式。两种方式各有各的优点,根据不同的情况进行选择即可。二、建立项目1)建立SingleView项目,命名为CustomTableViewCell。2)完成nib文件配置,View中只有一个UITableView控件,就是我们将要显示的表视图了。设置style为Grouped,因为我们要把两种自定义Cell的方法用不同的分区显示。别忘记配置表的代理,在此就不赘述了,有疑问的看看demo就懂了。三、使用nib文件自定义Cell1)建立自定义Cell的nib文件,在New File里面选择UserInterface中的Empty,取名为CustomCell。2)接着,拉出6个label,摆出如下图所示(当然你可以自由发挥),注意中间的”|”也是一个label。为了能够在代码中找到Cell里面的控件,我们还需要设置label的Tag标记。(写死的label就不用了,例如“类型:”)。设置Tag:3)拖完控件之后,我们需要设置nib文件的控制器,修改File’s Owner的Class为YGViewController(对应自己程序中的表视图控制器),点击File’s Owner,然后在身份检查器中输入YGViewController。注:一个控制器是可以加载多个nib文件的,这里我们的YGViewController就加载了YGViewController.nib和CustomCell.nib两个文件。只要配置好输出口和操作的链接,我们就能有条不紊的对多个nib进行操作了。下面设定CustomCell.nib的输出口,取名teaCell。-(UITableViewCell *)customCellWithOutXib:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath{
//定义标识符
static NSString *customCellIndentifier = @"CustomCellIndentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customCellIndentifier];
//定义新的cell
if(cell == nil){
//使用默认的UITableViewCell,但是不使用默认的image与text,改为添加自定义的控件
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:customCellIndentifier];
CGRect nameRect = CGRectMake(88, 15, 70, 25);
UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameRect];
nameLabel.font = [UIFont boldSystemFontOfSize:nameFontSize];
nameLabel.tag = nameT//设置tag,以便后面的定位
nameLabel.textColor = [UIColor brownColor];
[cell.contentView addSubview:nameLabel];
CGRect classTipRect = CGRectMake(88, 40, 40, 14);
UILabel *classTipLabel = [[UILabel alloc]initWithFrame:classTipRect];
classTipLabel.text = @"班级:";
classTipLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:classTipLabel];
CGRect classRect = CGRectMake(135, 40, 40, 14);
UILabel *classLabel = [[UILabel alloc]initWithFrame:classRect];
classLabel.tag = classT
classLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:classLabel];
CGRect stuNameTipRect = CGRectMake(88, 60, 40, 12);
UILabel *stuNameTipLabel = [[UILabel alloc]initWithFrame:stuNameTipRect];
stuNameTipLabel.text = @"学号:";
stuNameTipLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:stuNameTipLabel];
CGRect stuNameRect = CGRectMake(135, 60, 150, 14);
UILabel *stuNameLabel = [[UILabel alloc]initWithFrame:stuNameRect];
stuNameLabel.tag = stuNumberT
stuNameLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[cell.contentView addSubview:stuNameLabel];
CGRect imageRect = CGRectMake(15, 15, 60, 60);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:imageRect];
imageView.tag = imageT
//为图片添加边框
CALayer *layer = [imageView layer];
layer.cornerRadius = 8;//角的弧度
layer.borderColor = [[UIColor whiteColor]CGColor];
layer.borderWidth = 1;//边框宽度
layer.masksToBounds = YES;//图片填充边框
[cell.contentView addSubview:imageView];
//获得行数
NSUInteger row = [indexPath row];
//取得相应行数的数据(NSDictionary类型,包括姓名、班级、学号、图片名称)
NSDictionary *dic = [_stuArray objectAtIndex:row];
//设置图片
UIImageView *imageV = (UIImageView *)[cell.contentView viewWithTag:imageTag];
imageV.image = [UIImage imageNamed:[dic objectForKey:@"image"]];
//设置姓名
UILabel *name = (UILabel *)[cell.contentView viewWithTag:nameTag];
name.text = [dic objectForKey:@"name"];
//设置班级
UILabel *class = (UILabel *)[cell.contentView viewWithTag:classTag];
class.text = [dic objectForKey:@"class"];
//设置学号
UILabel *stuNumber = (UILabel *)[cell.contentView viewWithTag:stuNumberTag];
stuNumber.text = [dic objectForKey:@"stuNumber"];
//设置右侧箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureI}1、在cell=nil里面摆好控件的位置,设置好相应的tag,然后在设置数据时,通过cell.contentViewviewWithTag:Tag 的形式定位到对应的Cell里面的tag。2、通过NSUInteger row = [indexPath row]的到当前是哪一行,NSDictionary *dic = [_stuArrayobjectAtIndex:row];得到那一行的数据。demo下载地址:http://download.csdn.net/detail/yang9667杨光(atany)原创,转载请注明博主与博文链接,未经博主允许,禁止任何商业用途。博文地址:http://blog.csdn.net/yang8456211/article/details/博客地址:http://blog.csdn.net/yang8456211—— by atany本文遵循“署名-非商业用途-保持一致”创作公用协议
发表评论:
TA的最新馆藏[转]&

我要回帖

更多关于 xib加载cell 的文章

 

随机推荐