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

    1. read public static void readfile(String filepath) { BufferedReader br = null; try { String sCurre ...

  2. 【Android端 APP GPU过度绘制】GPU过度绘制及优化

    一.Android端的卡顿 Android端APP在具体使用的过程中容易出现卡顿的情况,比如查看页面时出现一顿一顿的感受,切换tab之后响应很慢,或者具体滑动操作的时候也很慢. 二.卡顿的原因 卡顿的 ...

  3. logback 常用配置详解(二) <appender>

    logback 常用配置详解(二) <appender> <appender>: <appender>是<configuration>的子节点,是负责写 ...

  4. php js => splice 数组 插入 功能

    php    array_splice 手册详解 array_splice   - 把数组中的一部分去掉并用其它值取代 参数 input 输入的数组. offset 如果 offset 为正,则从 i ...

  5. jQuery与Ajax的应用——《锋利的jQuery》(第2版)读书笔记3

    第6章 jQuery与Ajax的应用 jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$ ...

  6. oc 单例

    单例模式: //static id _instace; // //+ (id)allocWithZone:(struct _NSZone *)zone //{ // static dispatch_o ...

  7. NGUI 屏幕自适应(初始设定宽高800x480只支持比其大的屏幕)

    自适应讲解部分可以参考以下网址:http://www.xuanyusong.com/archives/2536,下面代码中提到的AdaptiveManualHeight()函数就是参考该文章的. 下面 ...

  8. JS鼠标移入,移出事件

    该事件的效果就像百度首页的设置选项,当鼠标移入,移出时的效果,废话不多说了,直接上码. <!DOCTYPE html><html lang="en">< ...

  9. TCP的拥塞控制

    1.引言 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就是防止 ...

  10. 转:HIBERNATE一些_方法_@注解_代码示例---写的非常好

    HIBERNATE一些_方法_@注解_代码示例操作数据库7步骤 : 1 创建一个SessionFactory对象 2 创建Session对象 3 开启事务Transaction : hibernate ...