纯代码编写的简单自定义UITableViewCell:

1.像处理普通视图一样处理Cell:

clsTableViewCell.h:

1 #import <UIKit/UIKit.h>
2
3 @interface clsTableViewCell : UITableViewCell
4 @property (nonatomic,strong) UILabel *label;
5 @property (nonatomic,strong) UIImageView *img;
6 @end

clsTableViewCell.m:

 1 #import "clsTableViewCell.h"
 2
 3 @implementation clsTableViewCell
 4 @synthesize img;
 5 @synthesize label;
 6
 7 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 8 {
 9     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
10     if (self) {
11         // Initialization code
12     }
13     return self;
14 }
15
16 - (id)init
17 {
18     self = [super init];
19     if(self)
20     {
21         [self defaultSetting];
22     }
23     return self;
24 }
25
26 - (void)awakeFromNib
27 {
28     // Initialization code
29 }
30
31 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
32 {
33     [super setSelected:selected animated:animated];
34
35     // Configure the view for the selected state
36 }
37 - (void)addSubViews
38 {
39     img = [[UIImageView alloc] init];
40     label = [[UILabel alloc] init];
41
42     [self.contentView addSubview:img];
43     [self.contentView addSubview:label];
44 }
45 - (void)setSubViews
46 {
47     label.text = @"默认标题";
48     label.backgroundColor = [UIColor clearColor];
49     [img setImage:[UIImage imageNamed:@"img"]];
50 }
51 - (void)layoutSubviews
52 {
53     img.frame = CGRectMake([img image].size.width + 10, 30, [img image].size.width, [img image].size.height);
54     label.frame = CGRectMake(10, 30, 100, 30);
55     self.contentView.frame = CGRectMake(0, 0, 320, 80);
56 }
57 - (void)defaultSetting
58 {
59     [self addSubViews];
60     [self setSubViews];
61 }

2.视图控制器使用这个Cell:

普通UIViewController的clsMainVC.h:

1 #import <UIKit/UIKit.h>
2
3 @interface clsMainVC : UIViewController<UITableViewDelegate,UITableViewDataSource>
4 {
5     UITableView *_tableView;
6 }
7 @end

clsMainVC.m:

 1 #import "clsMainVC.h"
 2 #import "clsTableViewCell.h"
 3
 4 @interface clsMainVC ()
 5 {
 6     NSArray *data;
 7 }
 8 @end
 9
10 @implementation clsMainVC
11
12 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
13 {
14     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
15     if (self) {
16         // Custom initialization
17     }
18     return self;
19 }
20
21 - (void)viewDidLoad
22 {
23     [super viewDidLoad];
24     data = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
25     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
26     [self.view addSubview:_tableView];
27     _tableView.delegate = self;
28     _tableView.dataSource = self;
29
30     // Do any additional setup after loading the view.
31 }
32
33 - (void)didReceiveMemoryWarning
34 {
35     [super didReceiveMemoryWarning];
36     // Dispose of any resources that can be recreated.
37 }
38 #pragma mark
39 #pragma mark tableview delegate
40 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
41 {
42     return 80;
43 }
44 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
45 {
46     return data.count;
47 }
48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
49 {
50
51     return 1;
52 }
53 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
54 {
55     NSLog(@"select %d",indexPath.row + 1);
56 }
57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
58 {
59 //  比较奇怪的是使用- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier 方法 就无法正常显示自己定义的Cell
60 //  [tableView  registerClass:[clsTableViewCell class] forCellReuseIdentifier:@"cell"];
61     clsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
62
63     if(nil == cell)
64     {
65         cell = [[clsTableViewCell alloc] init];
66     }
67     cell.label.text = [data objectAtIndex:indexPath.row];
68     if((indexPath.row + 1)% 2 == 0)
69     {
70         cell.contentView.backgroundColor = [UIColor greenColor];
71     }
72     return cell;
73 }
74
75 @end

3.显示效果:

4.自定义实现倒不难,只是不懂

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

这个方法的作用究竟是什么?为什么会出现无法正常显示自定义Cell的问题?

Developer Library的意思是使用这个方法注册自己写的Cell类吧?

registerClass:forCellReuseIdentifier:

Registers a class for use in creating new table cells.

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
Parameters
cellClass

The class of a cell that you want to use in the table.

identifier

The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.

Discussion

Prior to dequeueing any cells, call this method or the registerNib:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

Availability
  • Available in iOS 6.0 and later.
Declared In

UITableView.h

XCode5.1
Base SDK : iOS 6.1
问题先保留着。

转载于:https://www.cnblogs.com/sweetyqueen/p/3601805.html

PureCode--iOS--自定义UITableViewCell(含疑问)相关推荐

  1. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目 ...

  2. iOS自定义View 控件自动计算size能力

    iOS自定义View 控件自动计算size能力 背景 在使用 UILabel 和 UIImage 的时候,不用指定宽高约束,控件也不会报约束缺失,还可以根据内容自己确定适合的宽高,特别适合 Xib 和 ...

  3. ios cell点击对勾_带图像和对勾的iOS自定义TableView

    ios cell点击对勾 In this tutorial, we'll be developing an iOS Application that contains a custom TableVi ...

  4. iOS——自定义cell

    iOS--自定义cell 在写自定义cell怎么实现之前,先来看一下自定义cell的作用和用法,这一点远远比怎么实现有用的多,在进行了两天的网易云仿写后,才发现自己对自定义cell的理解完全是错的,按 ...

  5. iOS 自定义 UICollectionView汇总

    文章目录 引言 I .iOS上传图片视图的封装[支持删除和添加] 1.1 demo源码下载: 1.2 计算cell的高度 II.风险商户材料证明视图 2.1 cellV的高度计算 2.2 自定义col ...

  6. 一劳永逸,iOS 自定义 ActionSheet 封装流程

    原文链接:http://www.jianshu.com/p/cfb87a7db7b1 本文为 iOS 自定义视图封装<一劳永逸>系列的第四期,旨在提供封装思路,结果固然重要,但理解过程才最 ...

  7. 自定义UITableViewCell需注意的问题

    自定义cell,有下面几种方法 方法一: 在controller的.m中 1 @interface ViewController ()<UITableViewDataSource,UITable ...

  8. ios 自定义字体_如何仅用几行代码在iOS应用中创建一致的自定义字体

    ios 自定义字体 by Yuichi Fujiki 藤木雄一 In this article, you'll learn how to create a unified custom look th ...

  9. iOS自定义组与组之间的距离以及视图

    iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...

最新文章

  1. python格式化字符串语法_详解Python3 中的字符串格式化语法
  2. layui数据表格自定义复选框表头_解决LayUI数据表格复选框不居中显示的问题
  3. 汇编环境搭建(vs2010(2012)+masm32)
  4. 你居然还去服务器上捞日志,搭个日志收集系统难道不香么!
  5. MaskRCNN路标:TensorFlow版本用于抠图
  6. java poi excel 生成表格的工具封装
  7. mysql dns反向解析_Mysql DNS反向解析导致连接超时过程分析(skip-name-resolve)
  8. Python-----规范化开发
  9. Android Studio API 文档_下载与使用
  10. 单片机红外模块知识分享,理论是日后实战的基础
  11. 从Q-learning到PPO大全 深度强化学习总结和理解
  12. 1951-2021年至今全国气象数据(逐日、逐月、逐年)
  13. oracle存储过程备份,Oracle存储过程(二)
  14. python网页编辑器-Python Jupyter 网站编辑器
  15. PC端打开微信公众号文章 图片加载慢的解决方法
  16. 用python计算圆周率Π
  17. 300多张精美京剧脸谱,收藏~~
  18. 凤凰金融张震:互联网金融将进入3.0时代
  19. 关于网线连内网,无线连外网,内外网同时连通的方法探究
  20. Mysql数据恢复---闪回恢复

热门文章

  1. python 字符串转换
  2. 1.4通过时间的方向传播
  3. Pandas 文本数据方法 find( ) rfind( ) index( ) rindex( )
  4. R语言分类算法之朴素贝叶斯分类(Naive Bayesian Classification)
  5. vue 打开html流_解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题...
  6. 微型计算机天逸510s光驱,主机届的小钢炮,性能最强NAS——天逸510S Mini
  7. mysql proxy 读写分离_mysql-proxy 实现读写分离
  8. 什么是云原生?聊聊云原生的今生(转)
  9. 用vbs运行CMD不显示窗口的方法汇总
  10. c语言const限制什么,[C语言]类型限定词const解析