zoukankan      html  css  js  c++  java
  • UITableViewCell自定义cell

     

    设计好自定义的cell并且连接好控件后  有两种方法引用我们自己的cell

    方法1:

    复制代码
     1 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     2 {
     3     static NSString *cellIdentifier=@"name";
     4     BOOL nibsRegistered=NO;
     5     if (!nibsRegistered) {
     6         UINib *nib=[UINib nibWithNibName:@"MyCell" bundle:nil];
     7        [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
     8         nibsRegistered=YES;
     9     }
    10     MyCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    11 //cell 上的元素初始化代码
    12 
    13 return cell;
    14 }
    复制代码
     UINib *nib=[UINib nibWithNibName:@"MyCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
    这两句代码是引用我们自己定义的cell的关键 首先读取我们自己定义的cell的nib文件 再在tableView中注册 此时 我们定义的cell便加入
    到了tableView的可重用队列当中了
    MyCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    这句代码从中取出一个事例  然后初始化 并返回给tableView显示


    方法2:
    复制代码
     1 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     2 {
     3     static NSString *tableCellIdentifier = @"name";
     4     MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:tableCellIdentifier];
     5     
     6     if(cell == nil){
     7         NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"MyCell" owner:self options:nil];
     8         for(id oneObject in nib){
     9             if([oneObject isKindOfClass:[MyCell class]]){
    10                 cell = (MyCell *)oneObject;
    11             }
    12         }
    13     }
    14     //cell初始化。。。
    15     
    16     return cell;
    17 }
    复制代码
  • 相关阅读:
    数组是个好东西
    排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。
    子序列的和
    韩信点兵
    水仙花数
    阶乘之和
    3n+1问题
    MongoDB 安装
    mysql中bigint在php中表示
    Android之NDK开发
  • 原文地址:https://www.cnblogs.com/shuxiachahu123/p/5008075.html
Copyright © 2011-2022 走看看