纯代码编写的简单自定义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.

- (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
问题先保留着。
 

PureCode--iOS--自定义UITableViewCell(含疑问)的更多相关文章

  1. iOS 自定义UITableViewCell

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  2. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

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

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  4. 【转】iOS 通过xib自定义UITableViewCell【原创】

    原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...

  5. iOS学习之自定义UItableViewCell

    在项目开发中,大部分情况下我们都需要自定义UITableViewCell, 今天就重点整理一下目前自己已经学过的自定义Cell的一些注意事项; 分步骤来写吧: 1.将自定义的Cell定义为属性; 2. ...

  6. IOS开发---菜鸟学习之路--(七)-自定义UITableViewCell

    本篇将介绍如何自定义 UITableViewCell 首先选择新建文件 可以直接使用快捷键 COMMAND+n打开新建页面,然后选Objective-C class 然后选择继承之UITableVie ...

  7. 通过xib自定义UITableViewCell

    通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...

  8. 114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)

    关键操作: 效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UITableViewCo ...

  9. IOS中UITableViewCell使用详解

    IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...

随机推荐

  1. Android Dex文件格式(一)

    dex是Android平台上(Dalvik虚拟机)的可执行文件, 相当于Windows平台中的exe文件, 每个Apk安装包中都有dex文件, 里面包含了该app的所有源码, 通过反编译工具可以获取到 ...

  2. HDU2063 过山车

    过山车 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是, ...

  3. C语言获取时间

    转载:http://www.cnblogs.com/fzhe/archive/2012/11/06/2757858.html C语言获取系统时间的几种方式   C语言中如何获取时间?精度如何? 1 使 ...

  4. centos从日志文件查找关键字的日志并生成文件

    grep "unset user wechat user_id:" app* | tee wechat_log

  5. 使用PHP发送email进行账号激活或者密码修改操作

    使用PHPMailer编写发送邮件 PHPMailer需PHP的socket扩展支持,而PHPMailer链接qq域名邮箱时需要ssl加密方式(qq邮箱最近做了限制,新开域名邮箱不再允许通过smtp协 ...

  6. textFiled的placeHolder字体颜色

    self.title=@"修改UITextField的placeholder字体颜色"; UITextField *textTF=[[UITextField alloc]initW ...

  7. lvs+keepalived+nginx实现高性能负载均衡集群

    一.为什么要使用负载均衡技术? 1.系统高可用性 2.  系统可扩展性 3.  负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查, ...

  8. 添加了有道生词本的 chrome google翻译扩展和有道翻译扩展

    在chrome发布项目,需要先花美金认证,还得要美国ID,无奈. 直接上源码,需手动导入. 原始项目源码并未开源,个人是从chrome本地文件里拿出来的,拓展来的,侵删(本来想着自已写一个,业余时间, ...

  9. jQuery.cookie.js插件了解及使用方法

    jquery.cookie.js插件实现浏览器的cookie存储,该插件是基于jquery开发,方便cookie使用. jquerycookie.js的下载地址 http://plugins.jque ...

  10. MVC拦截器记录操作用户日志

    主要是用于记录用户操作动态, public class OperationAttribute:ActionFilterAttribute { /// <summary> /// 方法名称 ...