IOS学习之路七(通过xib自定义UITableViewCell)
2014-04-28 11:53
190 查看
一、新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCellDemo,则完成后自动生成代码视图如下图:
![](http://img.blog.csdn.net/20130806195323453?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
二。新建一个UITableViewCell文件:
![](http://img.blog.csdn.net/20130806195910750?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130806195938906?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
三。Add---New Files----User Interface-----Empty XIB
创建一个空的 MyTableViewCell.xib 文件,记住,XIB的名称一定要跟 签名的类的名称一致,也就是一模一样。
一定要选 Empty XIB类型,如果不是选的这个,那么创建的XIB里面的已经存在的那个UIView将不能调整高度,它的高度固定死了。
![](http://img.blog.csdn.net/20130806200018953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130806200123609?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
4.在xib中拖入一个Table View Cell 和一个label 一个imageView ,并于MyTableViewCell中连接如下图:
![](http://img.blog.csdn.net/20130806200227546?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130806200522984?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130807090005656?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lsZGNhdGxlbGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
五。这样,就可以往这个新添加的View里面添加我们自己的个性化控件了,这个View就是我们的Cell的模板了。这个过程跟普通的XIB一样,没有什么特别的。
那么如何在代码中使用这个MyTableViewCell呢?
代码如下:
MyTableViewCell类:
[cpp] view
plaincopyprint?
// MyTableViewCell.h
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (copy,nonatomic) NSString *titleName;
@property (copy,nonatomic) NSString *image;
@end
[cpp] view
plaincopyprint?
//
// MyTableViewCell.m
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import "MyTableViewCell.h"
@implementation MyTableViewCell
@synthesize imageView;
@synthesize titleLabel;
@synthesize titleName;
@synthesize image;
-(void)setImage:(NSString *)image{
self.imageView.image=[UIImage imageNamed:[image copy]];
}
-(void)setTitleName:(NSString *)titleName{
self.titleLabel.text=[titleName copy];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
ViewController类文件:
[cpp] view
plaincopyprint?
// ViewController.h
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *myTableView;
@end
[cpp] view
plaincopyprint?
//
// ViewController.m
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTableView=_myTableView;
#pragma mark -实现协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 40.0f;
if ([tableView isEqual:self.myTableView]){
result = 80.0f;
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell;
//定义CustomCell的复用标识,这个就是刚才在CustomCell.xib中设置的那个Identifier,一定要相同,否则无法复用
static NSString *identifier = @"MyTableViewCell";
//根据复用标识查找TableView里是否有可复用的cell,有则返回给cell
cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
//判断是否获取到复用cell,没有则从xib中初始化一个cell
if (!cell) {
//将Custom.xib中的所有对象载入
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
//第一个对象就是CustomCell了
cell = [nib objectAtIndex:0];
}
cell.image=@"1.jpeg";
cell.titleName=@"wildcat的专栏 新浪微博:http://weibo.com/u/3202802157";
return cell;
}
#pragma mark - Controller方法
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor redColor];
self.myTableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.myTableView.dataSource=self;
self.myTableView.delegate=self;
self.myTableView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.myTableView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.myTableView=nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
运行结果:
二。新建一个UITableViewCell文件:
三。Add---New Files----User Interface-----Empty XIB
创建一个空的 MyTableViewCell.xib 文件,记住,XIB的名称一定要跟 签名的类的名称一致,也就是一模一样。
一定要选 Empty XIB类型,如果不是选的这个,那么创建的XIB里面的已经存在的那个UIView将不能调整高度,它的高度固定死了。
4.在xib中拖入一个Table View Cell 和一个label 一个imageView ,并于MyTableViewCell中连接如下图:
五。这样,就可以往这个新添加的View里面添加我们自己的个性化控件了,这个View就是我们的Cell的模板了。这个过程跟普通的XIB一样,没有什么特别的。
那么如何在代码中使用这个MyTableViewCell呢?
代码如下:
MyTableViewCell类:
[cpp] view
plaincopyprint?
// MyTableViewCell.h
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (copy,nonatomic) NSString *titleName;
@property (copy,nonatomic) NSString *image;
@end
[cpp] view
plaincopyprint?
//
// MyTableViewCell.m
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import "MyTableViewCell.h"
@implementation MyTableViewCell
@synthesize imageView;
@synthesize titleLabel;
@synthesize titleName;
@synthesize image;
-(void)setImage:(NSString *)image{
self.imageView.image=[UIImage imageNamed:[image copy]];
}
-(void)setTitleName:(NSString *)titleName{
self.titleLabel.text=[titleName copy];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
ViewController类文件:
[cpp] view
plaincopyprint?
// ViewController.h
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *myTableView;
@end
[cpp] view
plaincopyprint?
//
// ViewController.m
// UITableViewCellDemo
//
// Created by WildCat on 13-8-6.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTableView=_myTableView;
#pragma mark -实现协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 40.0f;
if ([tableView isEqual:self.myTableView]){
result = 80.0f;
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell;
//定义CustomCell的复用标识,这个就是刚才在CustomCell.xib中设置的那个Identifier,一定要相同,否则无法复用
static NSString *identifier = @"MyTableViewCell";
//根据复用标识查找TableView里是否有可复用的cell,有则返回给cell
cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
//判断是否获取到复用cell,没有则从xib中初始化一个cell
if (!cell) {
//将Custom.xib中的所有对象载入
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
//第一个对象就是CustomCell了
cell = [nib objectAtIndex:0];
}
cell.image=@"1.jpeg";
cell.titleName=@"wildcat的专栏 新浪微博:http://weibo.com/u/3202802157";
return cell;
}
#pragma mark - Controller方法
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor redColor];
self.myTableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.myTableView.dataSource=self;
self.myTableView.delegate=self;
self.myTableView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.myTableView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.myTableView=nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
运行结果:
相关文章推荐
- IOS学习之路七(通过xib自定义UITableViewCell)
- IOS学习之路七(通过xib自定义UITableViewCell)
- IOS学习之路七(通过xib自定义UITableViewCell)
- 通过改变网络拓扑使用Sniffer Pro监控、分析协议的示意图
- 通过写Java代码来对MyEclipse进行注册
- linux下Oracle通过设置大内存页解决使用swap分区问题
- Java通过new Date() 得到的时间与系统时间差N个小时的解决办法
- android 通过iptables设置网络防火墙
- tomcat 责任链设计模式 底层源码剖析---如何通过请求(url)找到jsp、servlet文件
- eclipse 通过JDBC连接mysql server
- 黄聪:WordPress 多站点建站教程(五):获取子站点用户信息(通过输入站点ID号来获取该站点的所有用户)
- [Kali_BT]通过低版本SerialPort蓝牙渗透功能手机
- Android 通过代码设置radiobutton不同方位图标的两种方法
- 各种LG的5.0.1的root方法(VS985 24B亲测通过)
- 性能调优之mysql通过status性能优化 1
- 通过反射实现对象转JSON
- Jquery通过ajax请求NodeJS返回json数据实例
- 通过反编译的方式分析阿里手淘小蜜的实现方式
- linux中直接进行系统调用和通过C库调用的示例
- redis是key-value存储的,放在内存中,并在磁盘持久化的数据结构存储系统 通过set key value来存储,通过get key来获取值 复制代码 set key value:设