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

    WebSocket是H5提出的为了解决客户端和服务器双工通信的方法.在我们使用HTTP协议通信时,客户端主动向服务器发送请求,得到服务器端数据.为了与服务器端数据保持一致,之前常用的有轮询和长轮询两种 ...

  2. Redis学习笔记-进阶

    Redis持久化方案 redis有rdb和aof两种持久化方案 1)rdb方式 当符合一定条件时会自动将内存中的所有数据执行快照操作并存储到硬盘上 默认存储在redis根目录的dump.rdb文件中, ...

  3. Mac OS X的空间去哪儿了

    记得有事儿没事儿看下,/cores/目录的大小. cores目录是存放程序dump的数据,对于绝大部分人时是没有用的.可以删除.如果遇到坑爹的程序,一直生成dump文件,硬盘瞬间就爆满了. 典型:An ...

  4. C/C++入门基础----指针(1)

    指针其实就是一个变量, 和其他类型的变量一样.在32位计算机上, 指针占用四字节的变量.指针与其他变量的不同就在于它的值是一个内存地址,指向内存的另外一个地方, 指针能够直接访问内存和操作底层的数据, ...

  5. ABAP 字符串操作

    1).SHIFT:截断字符串 SHIFT {c} [BY {n} PLACES] [{mode}].: 作用:去掉字符串的前n个位置的字符,如果n未指定,默认为1,如果指定的n小于等于0,则字符串不变 ...

  6. (一)startup.bat

    那么startup.bat(// TODO startup.sh暂缓)启动后发生了什么呢? @echo off rem Licensed to the Apache Software Foundati ...

  7. sql数据查询,在程序中较慢,在MS SQL2005 Management Studio中速度快,情况分析及解决

    这两天遇到一个问题,在.net开发的网站,执行sql查询,从sql profiler中监控卡看,执行时间22s. 但是拷出的sql在Management Studio中直接执行,时间仅4ms. 解决方 ...

  8. 九月二十八JS验证

    js表单验证 js可用发来在数据被送往服务器前对HTML表单中的这些输入数据进行验证 被js验证的这些典型的表单数据有: >用户是否已填写表单中的必填项目: >用户输入的邮件地址是否是合法 ...

  9. AFN 无网络监控

    直接把封装到请求里就好 -(void)afn{ //1.创建网络状态监测管理者 AFNetworkReachabilityManager *manger = [AFNetworkReachabilit ...

  10. MJPhotoBrowser BUG修复

    崩溃在loading.progress = (float)receivedSize/expectedSize; 分析:MJPhotoView 执行了hide移除了MJPhotoLoadingView, ...