zoukankan      html  css  js  c++  java
  • 从xib 创建 collectionViewCell

    注意要 -- 注册 xib

    - (void)awakeFromNib {
    
        [super awakeFromNib];
        
        UINib *nib = [UINib nibWithNibName:@"MyPurchaseRecordFooterCell" bundle:[NSBundle mainBundle]];
        [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"MyPurchaseRecordId"];
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        // 在这里注册 nib 也可以 // UINib *nib = [UINib nibWithNibName:@"MyPurchaseRecordFooterCell" bundle:[NSBundle mainBundle]]; // [collectionView registerNib:nib forCellWithReuseIdentifier:@"MyPurchaseRecordId"]; MyPurchaseRecordFooterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyPurchaseRecordId" forIndexPath:indexPath]; cell.model = self.collectionModelM[indexPath.item]; return cell; }

    2.从xib 创建tableViewCell

    可以在创建tableview的时候,直接 注册nib

    self.tableView.backgroundColor = xxxx;
    [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
    这样你在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath这个方法里,你就可以省下这些代码:
     
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; 
    }
    而只需要

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    3.需不需要注册?
    使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;
    使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。

  • 相关阅读:
    阶段1 语言基础+高级_1-2 -面向对象和封装_2面向对象思想的举例
    阶段1 语言基础+高级_1-2 -面向对象和封装_1面向对象思想的概述
    2-3 【初识组件】顶部 TabBar
    2-2 工程源码文件结构
    Fragment状态保存
    【51单片机】六种亮灯方式
    Hadoop自学笔记(二)HDFS简单介绍
    lvs 负载均衡环境搭建
    [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第十一章 属性
    说说nio----1
  • 原文地址:https://www.cnblogs.com/yangzhifan/p/5594829.html
Copyright © 2011-2022 走看看