自定义UITableViewCell
来源:互联网 发布:s8新赛季符文 知乎 编辑:程序博客网 时间:2024/05/19 13:22
1。在XCode中选择新建->User Interface->Empty XIB。(指定一个有意义的名字)
2。打开空的xib文件,将UITableViewCell拖到xib窗口中。
3。添加样式和其他控件到里面去,(UITextField & UITextView 不适用于表格视图单元)
4。打开属性检查器,设置重用标识符号Identifier,如:BaseTableCell
#define TEXTLABEL ((UILabel *)[cell viewWithTag:101])
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//设置重用
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"BaseTableCell"];
if(!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"BaseTableCell" owner:self options:nil]lastObject];
}
//设置cell的背景
[cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"cell_bg.png"]]];
[TEXTLABEL setText:[[UIFont familyNames] objectAtIndex:indexPath.row]];
return cell;
}
//可以在以上代理中设置单元格的高度 tableView.rowHeight = 100;
//也可以在专门设置高度的代理方法中设置表单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return 100;
}
//自定义单元格背景颜色
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell setBackgroundColor:[UIColor redColor]]; //设置背景颜色
[cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"cell_bg.png"]]]; //设置选中后的背景,同前一个方法。
}
http://xiaohui3837843.blog.163.com/blog/static/54388740201151635857655/
----------------------------------------------------------------------------------
视图需要我们自己去定义样式时用到了TableViewCell组件,效果如下
首先创建View Based App工程,在.xib文件中拖入一个Table View,前面我们说到了,这里就不再重复,注意连接协议和两个必须方法的实现。完成.h中代码
- #import <UIKit/UIKit.h>
- @interface TableViewCellViewController : UIViewController
- <UITableViewDelegate,UITableViewDataSource>{
- IBOutlet UITableView *tView;
- }
- @property (nonatomic,retain)UITableView *tView;
- @end
完成.m中代码
- #import "TableViewCellViewController.h"
- #import "MyCell.h"
- @implementation TableViewCellViewController
- @synthesize tView;
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)dealloc {
- [tView release];
- [super dealloc];
- }
- -(NSInteger) tableView:(UITableView *)tableView
- numberOfRowsInSection:(NSInteger)section
- {
- return 9;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"CustomCellIdentifier";
- MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil];
- cell = [array objectAtIndex:0];
- [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
- }
- [[cell lable] setText:@"31"];
- [[cell lable1] setText:@"Raul"];
- [[cell myImage] setImage:[UIImage imageNamed:@"3316.jpg"]];
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 120;
- }
- @end
下面我们新建一个类,注意subclass选择UITableViewCell,名称为MyCell,生成之后再创建相应的xib文件
双击MyCell.xib,将Table View Cell拖入主窗口中,并且删除原主窗口中的View图标
在.h文件中完成代码
- #import <UIKit/UIKit.h>
- @interface MyCell : UITableViewCell {
- IBOutlet UILabel *lable;
- IBOutlet UILabel *lable1;
- IBOutlet UIImageView *myImage;
- }
- @property(nonatomic,retain) UILabel *lable;
- @property(nonatomic,retain) UILabel *lable1;
- @property (nonatomic,retain) UIImageView *myImage;
- @end
在.m中完成代码
- #import "MyCell.h"
- @implementation MyCell
- @synthesize lable,lable1,myImage;
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
- // Initialization code
- }
- return self;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- - (void)dealloc {
- [myImage release];
- [lable release];
- [super dealloc];
- }
- @end
最后我们看一下MyCell.xib中的连接,按住Ctrl拖入将要显示区域的文字和图片找到相应的接口即可。(注意:是myCell和文字和图片连接,不是file’s owner和文字和图片连接,我又犯这个错误了)
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell。。。。
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- UItableviewCell 自定义
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义uitableviewcell
- 自定义uitableviewcell++++
- 自定义UITableViewCell
- NAND Flash中常用的纠错方式
- 给你的站点全面提速——来自Yahoo UI的各种Bset Practices
- OCC class hierarchy
- 从《大数据的深意》开始
- memcached在大负载高并发网站上的应用(一)---简介
- 自定义UITableViewCell
- 批量编译目录下文件的Makefile
- 【數論】【搜索】【SCOI2009】遊戲
- 一个IOCP的小例子
- 让JComboBox有自动查找和完成功能
- 罗技摄像头c270无法在QQ上使用
- DRP 基础知识总结(一)
- memcached在大负载高并发网站上的应用(二)---应用场景
- 车辆监控系统使用常见问题