IOS学习之路七(通过xib自定义UITableViewCell)
2014-04-28 11:31
405 查看
一、新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCellDemo,则完成后自动生成代码视图如下图:
二。新建一个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
运行结果:
二。新建一个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
运行结果:
相关文章推荐
- 自定义uitableviewcell
- UITableView的组的自定义与cell的重用
- 自定义tabelviewcell
- UITableView 实现异步加载图片之改进
- [iOS7]IOS7中UITableView的变化
- UITableView(一)
- UITableView(一)
- iOS ios7的一些变化
- IOS UITableView 移动自定义cell
- 局部刷新UITableView
- UITableview cell之间间距设置
- tableviewcell重绘
- UItableviewCell 关于Index
- 粗糙的iOS笔记之一应用生命周期
- UITableViewCell 的重用解决
- 定制单元格
- UITableView应用1
- IOS SDK -UITableView的奇葩特性
- 关于TableViewCell 问题
- 自定义的tablecell,可以有不同大小么