纯代码编写的简单自定义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. iOS(本地通知与远程通知)

    iOS 推送通知有两种:本地推送.远程推送. 本地推送 :  在不需要联网的情况下,由APP发出推送,常用于某一时刻的通知,如闹钟.本地通送有局限性在于当APP处于后台或者退出时就无法发出通知. 远程 ...

  2. jquery实现淡入淡出

    fade方法包括四个: (1)fadeIn(speed,callback):淡入的方法,speed代表淡入的速度,可以是slow,fast,毫秒,不填等 例如: $(document).ready(f ...

  3. HTML5 新增属性和废除属性

    删除的属性大多都是可以用CSS替代的一些样式属性 设置网页标题前面的小图标的大小:size <link rel="icon" href="demo_icon.gif ...

  4. CSS布局设计

    CSS布局设计: (1)固定布局:各个部分采用固定宽度的页面布局. (2)流式布局:通过定义模块和模块间距的百分比的方式来实现.缺点是会自动缩放,影响图片的美观. (3)响应式布局:页面可以用户的设备 ...

  5. 产生某个区间的随机整数 int #Java

    int max = Integer.MAX_VALUE; int min = 1; Random random = new Random(); int s = random.nextInt(max)% ...

  6. [转]Oracle 经验集

    -- 转换字符串为日期格式 to_date('2011-12-30 11:54:30','yyyy-MM-dd:hh24:mi:ss') Oracel 用 C# 连接,Microsoft 自带的 Sy ...

  7. 创建并追加img元素(jquery!)

    有几种方法 但都需要你指定一个节点 根据这个节点进行添加 如现有一节点Id为pr:一,向该节点内部后方添加:1 $("#pr").append("<img src= ...

  8. [转] Oracle analyze table 使用总结

    转自:http://www.cnblogs.com/einyboy/archive/2012/08/09/2630321.html analyze table 一般可以指定分析: 表,所有字段,所有索 ...

  9. java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决(碰到问题,转载答案)

    自己前一段时间出现了这个问题,通过在网上搜索,大概知道了原因,整理下一,以供大家参考. 将项目部署好后,启动tomcat后报错,java.lang.NoClassDefFoundError: org/ ...

  10. 数组 Arrays类

    1.数组是一组变量集合,用来存储相同数据类型的一组数据的连续的空间. *数组名(标识符)连续空间首地址. *元素下标标明元素在数组中的位置,从0开始. *每个元素都可以通过下标来访问. *数组长度固定 ...