邯郸建设局公开网站,站长网站seo查询,湖南建筑信息网平台,电子商务网站建设步iOS学习 前言一#xff1a;TableView协议二#xff1a;cell的复用Cell的复用原理自定义cell总结 前言
UITableView是iOS开发中不可或缺的一部分#xff0c;它是苹果iOS SDK提供的一种用于展示数据列表的视图控件。我们对其中的cell进行自定义#xff0c;即可得到我们需要的… iOS学习 前言一TableView协议二cell的复用Cell的复用原理自定义cell总结 前言
UITableView是iOS开发中不可或缺的一部分它是苹果iOS SDK提供的一种用于展示数据列表的视图控件。我们对其中的cell进行自定义即可得到我们需要的UI界面
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath3.选择行的方法didSelectRowAtIndexPath- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath4.删除行的方法 :-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 一TableView协议
创建一个TableView我们首先需要加入UITableViewDelegate,UITableViewDataSource协议。其中UITableViewDataSource协议用来设置cell的行数列数。UITableViewDelegate协议用来设置cell的相关高度。 随后在.m文件中将TbaleView的委托对象和数据源对象设为自己。 _tableView.delegate self;_tableView.dataSource self;必须要实现的方法有如下几种
//分区的行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 行数
}
//列数
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{return 分区数;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return 单元格高度;
}
//单元格样式也是自定义cell需要修改的地方。
-(UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString *str cell;UITableViewCell *cell [_tableView dequeueReusableCellWithIdentifier:str];if (cell nil) {cell [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];} return cell;}剩余功能在项目中总结。
二cell的复用
cell的复用方式有两种注册和非注册此处给出代码来区分二者。 注册
//注册
- (void)viewDidLoad
{[super viewDidLoad];//使用Nib注册cell[self.tableView registerNib:[UINib nibWithNibName:CustomCell bundle:nil] forCellReuseIdentifier:myCell];// 使用代码注册cell[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:myCell];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString *identif mycell;UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:identifier];return cell;
}
非注册
- (void)viewDidLoad
{[super viewDidLoad];
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString *str cell;UITableViewCell *cell [_tableView dequeueReusableCellWithIdentifier:str];if (cell nil) {cell [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];}return cell;
}注册与非注册相比需要在viewDidLoad中先对cell类进行注册并且在其后的使用中不需要判空。 这是因为通过注册方法如registerClass:forCellReuseIdentifier:或registerNib:forCellReuseIdentifier:将指定的单元格类或Nib文件与标识符关联起来。通俗来讲即表格视图已经知道要用哪个cell。 而在非注册的方式中表格视图并不知道需要使用哪个单元格类或Nib文件。因此每次使用前都需要进行判空操作这是必要的。
Cell的复用原理
iOS内部使用了三个容器 _cachedCells, availableCells, _reusableCells 完成了 Cell 的复用。
cachedCells为NSMutableDictionary 类 是一个缓存容器用于存储表格视图中的已创建的单元格对象。每个单元格对象都与其对应的重用标识符相对应。当表格视图需要显示某个单元格时会首先在_cachedCells 中查找是否有可复用的单元格对象如果有则直接使用否则才会创建新的单元格对象。reusableCells 为NSMutableDictionary 是一个重用单元格的缓存容器用于存储可供重用的单元格对象。availableCells 类型NSMutableArray是一个可用单元格的缓冲池用于存储当前可用的、已显示在表格视图中的单元格对象。当单元格滚出屏幕时会被移动到availableCells 中等待复用。这样在需要显示新的单元格时可以首先尝试从 availableCells中获取可复用的单元格对象而不是每次都创建新的单元格。
重用优先级是先检查 reusableCells然后才是 cachedCells 中的缓存。 简而言之一开始出现的单元格和与其对应的键值对indexPath储存在cachedCells中。拷贝一份到availableCells中当有单元格出现在视图时先检查reusableCells中有没有可以复用的单元格没有则新建一个单元格并把顶部划出视图的单元格加入进reusableCells。 自定义cell
先给出效果 其中的每一栏就是一个自定义cell下面给出实现方法 创建一个继承与UITableViewCell的myCell。
interface myCell : UITableViewCell
property (nonatomic, strong) UIButton *Btn;
property (nonatomic, strong) UILabel *label1;
property (nonatomic, strong) UIImageView * imageView1;end
然后在.m文件中我们改写两个方法
-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{self [super initWithStyle:style reuseIdentifier:reuseIdentifier];_imageView1 [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];_imageView1.image [UIImage imageNamed:头像.jpeg];[self.contentView addSubview:_imageView1];[[NSNotificationCenter defaultCenter] addObserver:self selector:selector(receiveNotice:) name:notice object:nil];_label1 [[UILabel alloc] init];_label1.text 笙;_label1.textColor [UIColor blackColor];_label1.backgroundColor [UIColor clearColor];_label1.font [UIFont systemFontOfSize:25];[self.contentView addSubview:_label1];return self;
}-(void)layoutSubviews
{[super layoutSubviews];CGFloat screenWidth UIScreen.mainScreen.bounds.size.width;_label1.frame CGRectMake(100, 10, screenWidth, 90);_imageView1.frame CGRectMake(10, 10, 80, 80);
}
end其中-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{方法是修改cell的样式 -(void)layoutSubviews是修改布局视图。 我们还可以根据index的不同分区列在同一分区创建不同的的cell。这点在后续的博客中会记录。
总结
以上就是笔者学习的自定义cell和cell的复用有关内容多写代码不断遇到bug才会有更深入的理解。