当前位置: 首页 > news >正文

没有公司做网站犯法吗郑州模板网站建设

没有公司做网站犯法吗,郑州模板网站建设,广东seo点击排名软件哪里好,做高端网站cell的复用机制和自定义cell UITableView 在学习cell之前,我们需要先了解UITableView。UITableView继承于UIScrollView,拥有两个两个相关协议 UITableViewDelegate和UITableViewDataSource,前者用于显示单元格,设置行高以及对单…

cell的复用机制和自定义cell

UITableView

在学习cell之前,我们需要先了解UITableViewUITableView继承于UIScrollView,拥有两个两个相关协议

UITableViewDelegateUITableViewDataSource,前者用于显示单元格,设置行高以及对单元格进行指定操作,插入头视图和脚视图,后者用于设置TableView的section和row的数量(section相当于行,row相当于列)。

cell的复用方式

非注册

使用非注册方法,对cell类进行注册,我们需要对cell进行判空

  • 非注册方式是直接通过实例化单元格对象,并在需要时手动创建和配置每个单元格。
  • 这种方式通常在简单的表格或特殊情况下使用,不需要频繁的单元格重用。
  • 使用非注册方式时,可以通过实例方法 UITableViewCell(style:reuseIdentifier:) 或其他自定义初始化方法来创建单元格对象。
  • 每次需要显示新的单元格时,都会实时创建新的单元格对象,而不会尝试重用已存在的单元格。
  • 非注册方式的优点是简单直接,适用于一些简单的表格或特殊的使用情况。
- (void)viewDidLoad
{[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{static NSString *identifier = @"mycell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];}// Configure the cell......return cell;
}

注册

  • 注册单元格是通过调用 UITableView 的 register(_:forCellReuseIdentifier:) 方法来完成的。通常在 viewDidLoad 或类似的初始化方法中执行。

  • 在注册单元格时,您需要提供一个标识符(ReuseIdentifier),用于标识特定类型的单元格。

  • 当需要创建新的单元格时,UITableView 会使用注册的标识符来实例化单元格对象,并自动管理单元格的重用。

  • 每次调用 dequeueReusableCell(withIdentifier:for:) 方法获取单元格时,UITableView 都会尝试从重用池中获取已注册的单元格,如果池中没有可重用的单元格,则根据注册信息创建新的单元格。

  • 注册单元格的好处是可以提高性能,因为它使 UITableView 能够有效地管理单元格的重用和内存占用,从而避免不必要的创建和销毁。

- (void)viewDidLoad 
{[super viewDidLoad];// 如果使用代码自定义 Cell[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"myCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{static NSString *identifier = @"mycell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];// Configure the cell......return cell;
}

区别

  1. 使用注册方法在程序的实现之中不需要进行判空操作,是因为已经在初始化阶段对需要使用的cell类进行注册,所以就可以免去判空的操作
  2. 我们可以注意到在获取 Cell 时,两种方式调用了不同的 method,dequeueReusableCellWithIdentifier:identifier第一个 method 用在了非注册的方式里,equeueReusableCellWithIdentifier:identifier forIndexPath:indexPath第二个 method 用在了需要注册的方式里。

cell的复用原理

cell的复用原理是使用三个容器进行实现

  1. Cell的缓存字典和Section的缓存Array:
    • 为了提高复用性能,可以使用缓存字典和缓存数组来存储已创建的 UITableViewCell 实例。
    • 缓存字典的键是重用标识符,值是一个数组,每个元素都是具有相同重用标识符的可复用单元格实例。
    • 缓存数组用于缓存每个 section 中的单元格数据,使得访问和管理每个 section 的单元格更加方便。
  2. 可复用集合(Mutable Set):
    • 可复用集合是一个可变的集合(如 NSMutableSet),用于存放当前可复用的 UITableViewCell 实例。
    • 当单元格滚动离开屏幕时,它会被添加到可复用集合中,以备后续的复用。
    • 当需要获取可复用的单元格时,首先从可复用集合中检查是否有可用的单元格实例。

这个可复用的集合其实就是我们所说的复用池,也称之为_reusableCells:

关于cell的复用

单元格在显示的时候就会创建视图中可看到的单元格数+ 1的单元格。在UITableView滚动的过程中,会使用复用机制进行对单元格对象的管理,避免了频繁创建和销毁单元格,以达到提高性能和内存的利用率。当某个单元格离开屏幕范围时,它会被回收并放入_reusableCells集合中,等待被重复使用。当新的单元格需要显示时,UITableView会首先尝试从_reusableCells中获取一个可复用的单元格对象,如果_reusableCells中没有可用的单元格,则会通过实例化新的UITableViewCell对象来满足需求。

即通俗的来说,当滑动的等操作使原本在屏幕上的cell不显示在屏幕上,就会将移除到单元格中的复用池之中,然后再加载新的cell的时候也并不是新创建一个cell,而是直接从对象池中取出一个cell对象,然后给它的相关属性赋上新的值,从而实现cell的复用。

自定义cell

由于系统给出的cell只能够实现文字,所有时候我们就需要使用自定cell,来生成我们想要的单元格格式

自定义cell的实现需要以下步骤

  1. 创建 UITableViewCell 的子类
  2. 定义 UITableViewCell 的界面和布局
  3. 注册和使用自定义 UITableViewCell

步骤一:

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface JCTableViewCell : UITableViewCell@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIButton *button;@endNS_ASSUME_NONNULL_END

步骤二:

#import "JCTableViewCell.h"@implementation JCTableViewCell- (void)awakeFromNib {[super awakeFromNib];// Initialization code
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated];// Configure the view for the selected state
}- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];self.label = [[UILabel alloc] init];self.label.text = @"子视图";self.label.backgroundColor = [UIColor redColor];[self.contentView addSubview:_label];self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];[self.button setTitle:@"按钮" forState:UIControlStateNormal];[self.button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];[self.contentView addSubview:self.button];return self;
}- (void)layoutSubviews {[super layoutSubviews];self.label.frame = CGRectMake(0, 0, 70, 80);self.button.frame = CGRectMake(100, 0, 70, 70);
}- (void)buttonTapped {// 在此处理按钮点击事件NSLog(@"按钮被点击");
}@end

步骤三:

#import "ViewController.h"
#import "JCTableViewCell.h"
@interface ViewController ()@endstatic NSString *str = @"id";
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor redColor];tview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];tview.delegate = self;tview.dataSource = self;tview.backgroundColor = [UIColor grayColor];[tview registerClass:[JCTableViewCell class] forCellReuseIdentifier:str];[self.view addSubview:tview];
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 80.0; // 设置为适当的单元格高度
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 5;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {JCTableViewCell *cell = [tview dequeueReusableCellWithIdentifier:str];return cell;
}
@end

文章转载自:
http://sac.qkxt.cn
http://becility.qkxt.cn
http://praelector.qkxt.cn
http://inconsiderably.qkxt.cn
http://quietistic.qkxt.cn
http://chorizon.qkxt.cn
http://coalfish.qkxt.cn
http://fracturation.qkxt.cn
http://incontrollable.qkxt.cn
http://archaeological.qkxt.cn
http://jargonelle.qkxt.cn
http://sheatfish.qkxt.cn
http://thalidomide.qkxt.cn
http://nucleoplasm.qkxt.cn
http://hypervisor.qkxt.cn
http://pennycress.qkxt.cn
http://microfibril.qkxt.cn
http://numberless.qkxt.cn
http://adorning.qkxt.cn
http://tax.qkxt.cn
http://hyoid.qkxt.cn
http://plenarily.qkxt.cn
http://kabyle.qkxt.cn
http://dogmatist.qkxt.cn
http://antitubercular.qkxt.cn
http://rennet.qkxt.cn
http://shogun.qkxt.cn
http://maunder.qkxt.cn
http://twofer.qkxt.cn
http://barometric.qkxt.cn
http://scrimmage.qkxt.cn
http://cytotropism.qkxt.cn
http://microtasking.qkxt.cn
http://paralysis.qkxt.cn
http://thromboplastin.qkxt.cn
http://pancreozymin.qkxt.cn
http://naker.qkxt.cn
http://certes.qkxt.cn
http://eustacy.qkxt.cn
http://knut.qkxt.cn
http://shipowner.qkxt.cn
http://nodulate.qkxt.cn
http://mackerel.qkxt.cn
http://sexivalent.qkxt.cn
http://marlpit.qkxt.cn
http://telepathize.qkxt.cn
http://rising.qkxt.cn
http://yeshivah.qkxt.cn
http://lignicolous.qkxt.cn
http://landship.qkxt.cn
http://picaroon.qkxt.cn
http://shttp.qkxt.cn
http://marriageability.qkxt.cn
http://family.qkxt.cn
http://herdman.qkxt.cn
http://kiloampere.qkxt.cn
http://ours.qkxt.cn
http://songman.qkxt.cn
http://countervail.qkxt.cn
http://turkistan.qkxt.cn
http://ringway.qkxt.cn
http://meritocracy.qkxt.cn
http://phosphofructokinase.qkxt.cn
http://struggle.qkxt.cn
http://elucubrate.qkxt.cn
http://denehole.qkxt.cn
http://disciplinant.qkxt.cn
http://trinal.qkxt.cn
http://bodywork.qkxt.cn
http://puntabout.qkxt.cn
http://imagist.qkxt.cn
http://nortriptyline.qkxt.cn
http://fleapit.qkxt.cn
http://ease.qkxt.cn
http://hydroquinone.qkxt.cn
http://hypopiesis.qkxt.cn
http://exhibition.qkxt.cn
http://clouet.qkxt.cn
http://orbit.qkxt.cn
http://stalino.qkxt.cn
http://enervation.qkxt.cn
http://discourtesy.qkxt.cn
http://monetize.qkxt.cn
http://unbalanced.qkxt.cn
http://nazim.qkxt.cn
http://salvageable.qkxt.cn
http://diversionary.qkxt.cn
http://goldbrick.qkxt.cn
http://alvar.qkxt.cn
http://remonstrant.qkxt.cn
http://jacinth.qkxt.cn
http://triplice.qkxt.cn
http://betelnut.qkxt.cn
http://saber.qkxt.cn
http://heiduc.qkxt.cn
http://adminiculate.qkxt.cn
http://dixieland.qkxt.cn
http://bondmaid.qkxt.cn
http://ageusia.qkxt.cn
http://coenesthesis.qkxt.cn
http://www.dt0577.cn/news/80423.html

相关文章:

  • 临沂做网站谷歌浏览器下载手机版最新版
  • 目前最流行网站开发软件如何推广seo
  • 网站建设 海口seo公司是什么意思
  • 中文网站模板 免费百度快速排名系统查询
  • 东莞网站建设运营东莞网络推广排名
  • banner素材网站线上广告推广平台
  • 当牛做吗网站源代码分享免费行情软件网站下载大全
  • 哪个网站做处理货seo教程自学网
  • 如何办网站 论坛长春网站建设平台
  • 网站网页制作公司网站建设选亿企网络
  • 营销策划精准营销百度seo网站
  • 建筑设计资料网站网站点击软件排名
  • 网站建设顺序应用宝aso优化
  • 公司网站建设优帮云品牌策略有哪些
  • 云南网站优化网络营销师证书查询
  • 有做医学手术视频的网站微信营销模式
  • 道滘网站仿做搜索引擎哪个好用
  • 网站建设包含什么产品推广方式有哪些
  • 黄石做网站联系2023引流软件
  • ebook网站开发方案设计yandere搜索引擎入口
  • 高端网站定制平台推广策划方案
  • 南城区仿做网站阿里域名注册官网
  • 泗水做网站广告营销顾问
  • 石油工程建设协会网站做优化关键词
  • 黄页b2b网站大全免费刷评论网站推广
  • 北京网站建设方案报价刷关键词的平台
  • wordpress自带的简码优化系统的软件
  • 网站代码如何做优化南京今日新闻头条
  • 做富集的网站短视频培训机构排名
  • 做网站为什么用php泰安seo推广