我们以前通常会这样做

- (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 cell;

}
严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的,所以,需要修改啊,一种修改方式是使用如下ios5提供的新方式:
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
还有就是在xib页面设置好(ios5之前也可以用的)


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"];
break;

}
还有更常用的
-(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 cell;
}

- (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];
}

通过xib加载UITableViewCell的新方式的更多相关文章

  1. xib加载的两种方式

      •Xib文件的加载 Ø方法1 NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"AppView" owner:nil ...

  2. plist文件的读取和xib加载cell

    plist 文件读取 例如在工程里倒入了plist文件 在工程里需要用到plist文件里的信息,就需要把plist文件读取出来. 如程序: -(NSArray *)moreDataArr{ if (! ...

  3. IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

  4. 点评js异步加载的4种方式

    主要介绍了点评js异步加载的4种方式,帮助大家更全面的了解js异步加载方式,感兴趣的小伙伴们可以参考一下 js异步加载的4种方式,点评开始. <!DOCTYPE html> <htm ...

  5. APP中数据加载的6种方式-b

    我们看到的APP,往往有着华丽的启动界面,然后就是漫长的数据加载等待,甚至在无网络的时候,整个处于不可用状态.那么我们怎么处理好界面交互中的加载设计,保证体验无缝衔接,保证用户没有漫长的等待感,而可以 ...

  6. 从xib加载文件

    一般自定义View, 如果从xib加载文件, 定义一个类方法, 返回xib + (instancetype)dropdown { return [[[NSBundle mainBundle] load ...

  7. 1.引入必要的文件 2.加载 UI 组件的方式 4.Parser 解析器

    //引入 jQuery 核心库,这里采用的是 2.0 <scripttype="text/javascript"src="easyui/jquery.min.js& ...

  8. 忘记block格式 xib加载没有计算导航栏和tabbar的大小

    敲inlineBlock xib加载没有计算导航栏和tabbar的大小 /将这个属性改为no self.tabBarController.tabBar.translucent = NO; 判断优化,两 ...

  9. spring加载xml的六种方式

    因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6 ...

随机推荐

  1. 安装 vue.js和第一个hello world

    一.在自己的项目文件中使用npm下载vue npm install vue 二.在文件中引入vue.js 三.第一个hello world 注:scritpt代码必须写在html代码的下面

  2. Mac Pro 开机自启动 PHP-FPM,Nginx,MySql 等软件

    在Mac下安装好了PHP开发环境(PHP-FPM,Nginx,MySql), 想设置成开机自启动,原来以为和一般的Linux系统一样,也是在rc.d这样目录放置启动脚本.在网上查了一些资料,发现苹果应 ...

  3. Redis学习笔记一:数据结构与对象

    1. String(SDS) Redis使用自定义的一种字符串结构SDS来作为字符串的表示. 127.0.0.1:6379> set name liushijie OK 在如上操作中,name( ...

  4. caffe学习系列(3):数据层介绍

    一个模型由多个层构成,如Data,conv,pool等.其中数据层是模型的最底层,是模型的入口. 提供数据的输入,也提供数据从Blobs转换成别的格式进行保存输出还包括数据的预处理(如减去 均值, 放 ...

  5. jdk 1.7 在ubuntu 环境配置

    在/opt/里解压了jdk 1.7后 设置环境变量 chen@caicai ~ $ vim .profile export JAVA_HOME=/opt/jdk1..0_79 export JRE_H ...

  6. 4-python学习——数据操作

    4-python学习--数据操作 参考python类型转换.数值操作(收藏) Python基本运算符 数据类型转换: 有时候,可能需要执行的内置类型之间的转换.类型之间的转换,只需使用类名作为函数. ...

  7. php之CI框架多语言的用法

    public function index() { // 加载语言包,可以加载多个 $this->lang->load('email'); echo $this->lang-> ...

  8. 【GoLang】golang 交叉编译 实现&工具

    apt-get install gcc-mingw-w64 env CGO_ENABLED= GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc g ...

  9. windows下安装Apache 64bit

    文件下载:http://pan.baidu.com/s/1c0oDjFE 一.Apache的安装 http://www.blogjava.net/greatyuqing/archive/2013/02 ...

  10. Python模块之optparse

    参考: http://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html https://docs.python.org/2/li ...