PureCode--iOS--自定义UITableViewCell(含疑问)
纯代码编写的简单自定义UITableViewCell:
1.像处理普通视图一样处理Cell:
clsTableViewCell.h:
#import <UIKit/UIKit.h> @interface clsTableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIImageView *img;
@end
clsTableViewCell.m:
#import "clsTableViewCell.h" @implementation clsTableViewCell
@synthesize img;
@synthesize label; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
} - (id)init
{
self = [super init];
if(self)
{
[self defaultSetting];
}
return self;
} - (void)awakeFromNib
{
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated]; // Configure the view for the selected state
}
- (void)addSubViews
{
img = [[UIImageView alloc] init];
label = [[UILabel alloc] init]; [self.contentView addSubview:img];
[self.contentView addSubview:label];
}
- (void)setSubViews
{
label.text = @"默认标题";
label.backgroundColor = [UIColor clearColor];
[img setImage:[UIImage imageNamed:@"img"]];
}
- (void)layoutSubviews
{
img.frame = CGRectMake([img image].size.width + , , [img image].size.width, [img image].size.height);
label.frame = CGRectMake(, , , );
self.contentView.frame = CGRectMake(, , , );
}
- (void)defaultSetting
{
[self addSubViews];
[self setSubViews];
}
2.视图控制器使用这个Cell:
普通UIViewController的clsMainVC.h:
#import <UIKit/UIKit.h> @interface clsMainVC : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
UITableView *_tableView;
}
@end
clsMainVC.m:
#import "clsMainVC.h"
#import "clsTableViewCell.h" @interface clsMainVC ()
{
NSArray *data;
}
@end @implementation clsMainVC - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
data = [[NSArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , , )];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self; // Do any additional setup after loading the view.
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
#pragma mark tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return data.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ return ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"select %d",indexPath.row + );
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 比较奇怪的是使用- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier 方法 就无法正常显示自己定义的Cell
// [tableView registerClass:[clsTableViewCell class] forCellReuseIdentifier:@"cell"];
clsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if(nil == cell)
{
cell = [[clsTableViewCell alloc] init];
}
cell.label.text = [data objectAtIndex:indexPath.row];
if((indexPath.row + )% == )
{
cell.contentView.backgroundColor = [UIColor greenColor];
}
return cell;
} @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.
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
niland 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
PureCode--iOS--自定义UITableViewCell(含疑问)的更多相关文章
- iOS 自定义UITableViewCell
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- 【转】iOS 通过xib自定义UITableViewCell【原创】
原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...
- iOS学习之自定义UItableViewCell
在项目开发中,大部分情况下我们都需要自定义UITableViewCell, 今天就重点整理一下目前自己已经学过的自定义Cell的一些注意事项; 分步骤来写吧: 1.将自定义的Cell定义为属性; 2. ...
- IOS开发---菜鸟学习之路--(七)-自定义UITableViewCell
本篇将介绍如何自定义 UITableViewCell 首先选择新建文件 可以直接使用快捷键 COMMAND+n打开新建页面,然后选Objective-C class 然后选择继承之UITableVie ...
- 通过xib自定义UITableViewCell
通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...
- 114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)
关键操作: 效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UITableViewCo ...
- IOS中UITableViewCell使用详解
IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...
随机推荐
- [lucene系列笔记1]lucene6的安装与配置(Windows系统)
lucene是一个java开源的高效全文检索工具包,最近做项目要用到,把学习的过程记录一下. 第一步:下载安装jdk 1.首先从官网下载jdk(下载之前先查看你的电脑是多少位操作系统,如果是32就下载 ...
- map集合键值对存储,键值不重复,值可以重复
import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Li ...
- ListView条目的侧拉删除
功能实现:类似QQ聊天记录中条目,左滑,可以删除该条目的功能:后续可以在自己的项目中添加该功能. 步骤一: (1)定义主布局,里面包含一个简单的ListView:命名id="lv" ...
- 【Android端APP 安装包检查】安装包检查具体内容及实现方法
一.安装包检查的具体包含内容有哪些? 1.安装包检查的一般内容包括: 安装包基本信息检查: 文件大小: xx MB 包名: com.xx 名称: xx 本次安装包证书与外网证书对比一致性:是 版本号 ...
- JAVA异常初步
1,1个图.Throwable是所有异常类的老祖宗,万恶之源.Error正常是系统级错误,控制不了,Exception类又分RuntimeException及别的异常,RuntimeException ...
- ubuntu远程桌面连接windows系统(转)
现在用ubuntu系统,公司买了个windows的服务器,需要给配置一套环境,来回跑很麻烦,就想windows下可以的远程桌面,linux应该也有. 现在自己的ubuntu13.10,无法进入桌面的& ...
- asp.net web.config 设置Session过期时间
在Asp.net中,可以有四处设置Session的过期时间:(原文作者:望月狼地址:http://www.cnblogs.com/wangyuelang0526/) 一.全局网站(即服务器)级 IIS ...
- Codeforces Round #381 (Div. 2) 复习倍增//
刷了这套题 感触良多 我想 感觉上的差一点就是差很多吧 . 每次都差一点 就是差很多了... 不能气馁..要更加努力去填补那一点点. 老天不是在造物弄人,而是希望你用更好的自己去迎接自己. A. ...
- 空MVC项目找不到System.Web.Optimization的处理办法
install-package Microsoft.AspNet.Web.Optimization Create the bundle in Global.asax Application_Start ...
- Ceph剖析:Leader选举
作者:吴香伟 发表于 2014/09/11 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 Paxos算法存在活锁问题.从节点中选出Leader,然后将所有对数据 ...