我们以前通常会这样做

- (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. 【C语言入门教程】5.5 实现问题(效率)

    在设计函数时需要遵循一些基本原则,因为影响到函数的执行效率和可用性.函数是代码复用的基础,一个健壮的函数或由函数组成的函数集可以在多个程序中使用.C语言标准库里存放的就是这样的函数,这些函数被放置在头 ...

  2. Objective-C上地球坐标系到火星坐标系转换算法

    Objective-C上地球坐标系到火星坐标系转换算法 http://blog.csdn.net/zhaoxy_thu/article/details/17033347

  3. centos 安装mysql

    wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-rele ...

  4. 查看SQL Server日志 Part 1

    曾经有朋友问我数据被删除了,不借助第三方工具能不能查是什么时候发生的. SQL Server提供了一个undocumented的函数fn_dblog可以让我们查看活动的transaction log. ...

  5. java使用split分隔,需要注意的点

    String severName = "10.6.62.244"; System.out.println(severName.split(".").length ...

  6. 实战Centos系统部署Codis集群服务

    导读 Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (不支持的命令列表), 上层应用可 ...

  7. php服务端写日志文件

    1.需求 在服务端记录日志 2.基础版 最基础的文件读写,(要注意window和linux的换行符,window是\r\n,linux是\n),这里就写入一个时间. <?php $handle ...

  8. 跟着百度学PHP[4]-OOP面对对象编程-1-什么是面对对象编程

    该阶段学习参考:http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/09/10/1823042.html 面向对象编程(Object Orie ...

  9. Unity响应Android的返回键,退出当前Activity

    一:使用 Application.Quit() public void Update() { if(Input.GetKeyDown(KeyCode.Escape)) Application.Quit ...

  10. Struts2中的OGNL通配符

    <action name="*_*" class="action.{1}Action" method="{2}"> 匹配,第一个 ...