UITableView的基本使用三(自定义Cell)
来源:互联网 发布:郑州七中分校知乎 编辑:程序博客网 时间:2024/04/24 05:09
在项目中,我们经常会使用UITableView,但是tableView中的cell格式又不一定每次都是一样的,所以我们需要自己实现我们自定义的cell,而自定义cell的方式也由很多,我们先采取用Xib的方式。
1.通过xib创建一个cell,并且通过tag来获取cell上的其他子元素,这种方式会产生很多的tag,让tag都暴漏在控制器中,不方便管理,而且viewWithTag性能低下,每次使用的时候,会遍历所有tag。
部分代码:
// tag比较多不容易管理 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * ID=@"CELL"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; if (cell==nil) { // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:ID]; //通过xib文件加载cell cell= [[[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:nil options:nil] lastObject]; //绑定监听器 UIButton *cool=(UIButton *)[cell viewWithTag:3]; [cool addTarget:self action:@selector(coolectBook:event:) forControlEvents:UIControlEventTouchUpInside]; } //覆盖shuj Book *book=self.books[indexPath.row]; //拿出第一个cell UILabel *nameLable=(UILabel *)[cell viewWithTag:1]; nameLable.text=book.name; UILabel *priceLable=(UILabel *)[cell viewWithTag:2]; priceLable.text=[NSString stringWithFormat:@"¥%.1f",book.price]; NSLog(@"%p",cell); return cell;}
通过触控点的位置获取所在行
//根据位置获取所在的行-(void)coolectBook:(UIButton *)btn event:(UIEvent *)event //包含所有的触摸点{ UITableView *tableView=(UITableView *)self.view; NSSet *touches = [event allTouches]; //包含所有的uitouch 单点触控就只有一个uitouch UITouch *touch= [touches anyObject]; //获取触摸点在uitableVIEW上的位置 CGPoint p=[touch locationInView:tableView]; NSIndexPath *path= [tableView indexPathForRowAtPoint:p]; Book *book=self.books[path.row]; NSLog(@"row--%@",book.name);}
2.利用loadNibNamed加载xib文件的时候把控制器传入owner,让xib的file's owner的custom class为控制器,也就是控制器充当监听器,这样控制器就可以监听到自定义cell的方法,这样做,耦合度比较高,不利于维护和扩展,自定义的cell只能被一个控制器使用。
部分代码:
NSArray *objs= [[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil] ;
3.我们自己以面向对象的方式,把关于自定义cell的所有的元素都封装在一个类里面,写一个子类继承自UITableViewCell,并把xib创建的cell的custom class改为子类的名称,这样我们加载xib生成的cell对象就是我们的子类对象。
封装的子类cell
#import <UIKit/UIKit.h>/* 把一个cell上的元素封装在一个整体 只提供get方法 */ @interface BookCell : UITableViewCell@property (weak, nonatomic,readonly) IBOutlet UILabel *nameLable;@property (weak, nonatomic,readonly) IBOutlet UILabel *priceLable;@property (weak, nonatomic,readonly) IBOutlet UIButton *BuyBtn;@end
注意:这里我们要使用readonly让外界只能访问我们内部的组件。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * ID=@"CELL"; BookCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; if (cell==nil) { // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:ID]; //通过xib文件加载cell NSArray *objs= [[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil] ; NSLog(@"%@",[objs lastObject]); cell=[objs lastObject]; //绑定监听器 //为子类的btn对象绑定事件 [cell.BuyBtn addTarget:self action:@selector(buyBook) forControlEvents:UIControlEventTouchUpInside]; } //覆盖数据 Book *book=self.books[indexPath.row]; cell.nameLable.text=book.name; cell.priceLable.text=[NSString stringWithFormat:@"%.1f",book.price]; return cell;}通过这种方式,我们自定义的cell很灵活,就像一个插件一样,如果其他地方要使用,只用导入我们的.h文件就可以了。
4.纯代码添加自定义的cell,子类会重写父类的initWithStyle方法用来初始化cell的样式
.h文件
#import <UIKit/UIKit.h>@interface MsgCell : UITableViewCell@property(nonatomic,weak,readonly) UIImageView * icon;@property(nonatomic,weak,readonly) UIButton * messageBtn;@end
#import "MsgCell.h"@implementation MsgCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { UIImageView *icon=[[UIImageView alloc]init]; icon.frame=CGRectMake(10, 10, 50, 50); [self.contentView addSubview:icon]; //[self addSubview:icon]; _icon=icon; UIButton *btn=[[UIButton alloc]init]; btn.frame=CGRectMake(80, 10, 200, 60); [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_focused.png"] forState:UIControlStateHighlighted]; [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_normal.png"] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.contentView addSubview:btn]; _messageBtn=btn; } return self;}@end
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID=@"cell"; MsgCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; if (cell==nil) { cell=[[MsgCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.icon.image=[UIImage imageNamed:@"1.jpg"]; [cell.messageBtn setTitle:@"dfgdfgdfg" forState:UIControlStateNormal]; return cell;}
0 0
- UITableView的基本使用三(自定义Cell)
- UITableView使用自定义cell的例子
- UITableView 的 自定义cell
- UITableView的自定义cell
- Swift_ uitableview使用自定义(xib)cell
- Swift UITableView使用自定义cell
- 爬爬爬之路:UI(十一) UITableView(三) 自定义Cell 多Cell混合 自适应高度 及cell的状态控制
- UITableView cell 的动态使用
- Swift编程(一):UITableView及自定义Cell的Xib
- UITableView-的cell 自定义 高度,样式
- 纯代码实现自定义UITableView的cell
- 几种UITableview自定义Cell的办法
- UITableView的自定义cell方法总结
- 几种UITableview自定义Cell的办法
- iOS代码自定义UITableView Cell(每个Cell的高度不一样)
- UITableView自定义Cell
- ios UITableview自定义cell
- <四>UITableView 自定义cell
- ios学习之universal
- Hadoop集群环境搭建(win7下vmware虚拟ubuntu)
- hdu 大菲波数
- Python 的join函数
- 树木品种-Qsort
- UITableView的基本使用三(自定义Cell)
- python操作MySQL数据库
- <<Oracle数据库性能优化艺术(第五期)>> 第15周 基于Oracle RAC架构的性能优化
- singleton
- 模态对话框和非模态对话框
- [Leetcode] Word Break (Java)
- 【序一】由nodejs+mongodb开发的ideas小站
- 照片Urls
- 【HPC】MIC和GPU在高性能计算中的使用