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

什么网站做优化最好种子搜索引擎 磁力天堂

什么网站做优化最好,种子搜索引擎 磁力天堂,上海正规网站制作价格,全球域名最贵的100个域名目录 前言cell的复用手动(非注册)自动(注册) 自定义cell总结 前言 Cell复用和自定义Cell是在开发iOS应用时常见的一种优化技巧和定制需求。   Cell复用是UITableView或UICollectionView的一个重要优化机制。当用户滚动这些视图时…

目录

  • 前言
  • cell的复用
    • 手动(非注册)
    • 自动(注册)
  • 自定义cell
  • 总结

前言

  Cell复用和自定义Cell是在开发iOS应用时常见的一种优化技巧和定制需求。
  Cell复用是UITableView或UICollectionView的一个重要优化机制。当用户滚动这些视图时,只有少量可见的Cell会被实际创建和显示,对于那些暂时不可见的Cell,系统会将它们缓存起来以备将来复用。这个机制主要是为了提高应用的性能,因为创建和销毁视图都是相对高开销的操作,通过复用Cell,我们可以避免不必要的视图创建和销毁,从而提高应用的滚动性能
  自定义Cell可以让你更好地控制Cell的外观和行为,提高代码的可读性和可维护性。自定义Cell主要的步骤包括创建自定义Cell类,添加UI元素,实现初始化方法,设置Cell的布局,以及在TableView中使用自定义Cell。
  在实际开发中,我们通常会结合使用Cell复用和自定义Cell,以达到既优化性能又满足特定需求的目的。

cell的复用

  Cell的复用是一种优化技术,主要用于iOS的UITableView和UICollectionView。需要注意的是,虽然这两种视图的实现方式略有不同,但复用的基本思想是相同的。
  当用户滚动UITableView或UICollectionView时,屏幕上显示的cell只是所有数据的一小部分。当某个cell滚动出屏幕时,系统会将其放入一个队列中等待复用,而不是立即销毁。当需要显示新的cell时,系统首先会检查这个队列,看看是否有可以复用的cell。如果有,就直接使用,如果没有,才会创建新的cell。
  这种复用机制可以极大地提高应用的性能。因为创建和销毁视图是相对耗费资源的操作,通过复用,可以减少这些操作,从而使滚动更加流畅。
  在实现cell复用时,需要给cell设定一个复用标识符(reuse identifier),然后在需要新的cell时,使用这个标识符去请求。如果队列中有可复用的cell,系统就会返回一个,否则就会创建新的cell。标识符的设定,使得我们可以为不同类型的cell设定不同的复用标识符,从而在同一个表视图或集合视图中使用多种类型的cell。

手动(非注册)

手动进行Cell复用主要涉及到以下几个步骤:

  1. 设置复用标识符:在创建Cell的时候,我们需要给每个Cell设置一个复用标识符,这个标识符通常是一个字符串,用来表示这个Cell的类型。在创建Cell的时候,我们会把这个标识符作为参数传入。
  2. 请求重用的Cell:在需要显示新的Cell时,我们会使用复用标识符去请求一个已经不再显示,但是还没有被销毁的Cell。这个请求的过程是通过调用UITableView或UICollectionView的dequeueReusableCell(withIdentifier:)方法来完成的,这个方法会返回一个可选类型的Cell,如果有可用的重用Cell,就会返回一个Cell,否则返回nil。
  3. 配置Cell:无论是新创建的Cell还是重用的Cell,都需要进行配置,以显示新的数据。配置Cell通常会在tableView(:cellForRowAt:)或collectionView(:cellForItemAt:)方法中完成。

代码示例:

 1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *strID = @"id";UITableViewCell *cell = [_tabView dequeueReusableCellWithIdentifier: strID];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: strID];}cell.textLabel.text = @"aaa";return cell;
}

自动(注册)

在iOS开发中,TableView和CollectionView的Cell复用是自动完成的,你只需要正确地设置复用标识符并在需要时请求复用的Cell。具体步骤如下:

  1. 设置复用标识符:当你创建自定义Cell的时候,需要为每一个Cell类型设置一个唯一的复用标识符。你可以在Cell的初始化方法中或者在Storyboard中设置这个标识符。
  2. 请求重用的Cell:在tableView(:cellForRowAt:)或collectionView(:cellForItemAt:)方法中,你需要使用复用标识符来请求一个可复用的Cell。你可以使用dequeueReusableCell(withIdentifier:)方法来完成这个请求。如果有可复用的Cell,这个方法会返回一个Cell,否则返回nil。
  3. 创建新的Cell:如果dequeueReusableCell(withIdentifier:)方法返回nil,说明没有可复用的Cell,你需要创建一个新的Cell。
  4. 配置Cell:对于获得的Cell,无论是新创建的还是复用的,你都需要按照当前的数据来配置它们。

代码示例:

 1. (void)viewDidLoad 
{[super viewDidLoad];// 如果使用 Nib 自定义 Cell[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"myCell"];// 如果使用代码自定义 Cell[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"myCell"];
}

自定义cell

自定义Cell是在开发iOS应用时常用的一种方式,它能让你更好地控制Cell的外观和行为,提高代码的可读性和可维护性。自定义Cell主要的步骤包括:

  1. 创建自定义Cell类:首先,需要创建一个新的类,这个类通常会继承自UITableViewCell或UICollectionViewCell。
  2. 添加UI元素:在这个自定义Cell类中,我们可以添加你需要的UI元素,如UILabel,UIImageView等。
  3. 实现初始化方法:在自定义Cell类的初始化方法中,需要初始化我们添加的UI元素,并添加到Cell的contentView上。
  4. 设置Cell的布局:还需要在自定义Cell类中设置UI元素的布局,可以使用Auto Layout来完成这个任务。
  5. 在TableView中使用自定义Cell:在TableView的tableView(_:cellForRowAt:)方法中,我们需要先通过复用标识符尝试获取一个可复用的Cell,如果没有获取到,那么就创建一个新的自定义Cell实例,并返回。

通过自定义Cell,我们可以根据自己的需求来定制Cell的外观和行为,使我们的应用更具个性化。

代码示例:

先创建一个子类myCell,从属于UITableViewCell类。
在这里插入图片描述
myCell.h:

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface myCustomCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@endNS_ASSUME_NONNULL_END

myCell.m:

#import "myCell.h"@implementation myCustomCell- (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];if ([reuseIdentifier isEqualToString:@"indentifierCell"]) {_titleLabel = [[UILabel alloc] init];_titleLabel.textColor = [UIColor blueColor];_titleLabel.font = [UIFont systemFontOfSize:20];[self.contentView addSubview:_titleLabel];_subtitleLabel = [[UILabel alloc] init];_subtitleLabel.textColor = [UIColor cyanColor];_subtitleLabel.font = [UIFont systemFontOfSize:15];[self.contentView addSubview:_subtitleLabel];}return self;
}
//重写布局方法,根据需求自己设置
- (void)layoutSubviews {_titleLabel.frame = CGRectMake(40, 20, self.contentView.bounds.size.width - 40, 20);_subtitleLabel.frame = CGRectMake(40, 40, self.contentView.bounds.size.width - 40, 20);
}
@end

ViewController .h:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController
<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>
{//定义一个数据视图对象//数据视图用来显示大量相同格式的信息的视图UITableView* _tableview;
}@end

ViewController .m:

#import "ViewController.h"
#import "myCell.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//创建数据视图_tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];//设置数据视图的代理对象_tableview.delegate = self;//设置数据视图的数据源对象_tableview.dataSource = self;//注册子类[_tableview registerClass:[myCustomCell class] forCellReuseIdentifier:@"indentifierCell"];[self.view addSubview:_tableview];
}
// 设置数据视图的组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}
//获取每组元素的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 15;
}
//创建单元格对象函数
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {myCustomCell* cell = [_tableview dequeueReusableCellWithIdentifier:@"indentifierCell" forIndexPath:indexPath];cell.titleLabel.text = @"一级标题";cell.subtitleLabel.text = @"二级标题";return cell;
}
@end

总结

  通过对cell的复用和自定义cell,我们可以对自己写的页面进行更多的个性化设置。以上就是本篇博客关于cell复用和自定义cell的全部内容,欢迎大家学习和指正~


文章转载自:
http://wanjiademagnetization.pfbx.cn
http://wanjiaroady.pfbx.cn
http://wanjiamonogram.pfbx.cn
http://wanjiatrackside.pfbx.cn
http://wanjiaketo.pfbx.cn
http://wanjialeafage.pfbx.cn
http://wanjiasemifeudal.pfbx.cn
http://wanjiaadditive.pfbx.cn
http://wanjiaexpiate.pfbx.cn
http://wanjiaantimissile.pfbx.cn
http://wanjiaaeolotropy.pfbx.cn
http://wanjiablub.pfbx.cn
http://wanjiacurable.pfbx.cn
http://wanjiahawash.pfbx.cn
http://wanjiaposter.pfbx.cn
http://wanjiacrowbill.pfbx.cn
http://wanjiacinemascope.pfbx.cn
http://wanjiashox.pfbx.cn
http://wanjiadisseminator.pfbx.cn
http://wanjiacrinkleroot.pfbx.cn
http://wanjiaunbaked.pfbx.cn
http://wanjiacomtist.pfbx.cn
http://wanjiatbm.pfbx.cn
http://wanjiabombshell.pfbx.cn
http://wanjiagraf.pfbx.cn
http://wanjiareliability.pfbx.cn
http://wanjiafremdness.pfbx.cn
http://wanjiaplowing.pfbx.cn
http://wanjialogaoedic.pfbx.cn
http://wanjiaspuria.pfbx.cn
http://wanjiairreproachably.pfbx.cn
http://wanjiasaccharify.pfbx.cn
http://wanjiaautarchist.pfbx.cn
http://wanjiabenzosulphimide.pfbx.cn
http://wanjiasammy.pfbx.cn
http://wanjiadecamerous.pfbx.cn
http://wanjiacatadromous.pfbx.cn
http://wanjiabicomponent.pfbx.cn
http://wanjiawineskin.pfbx.cn
http://wanjiatissue.pfbx.cn
http://wanjiaguisard.pfbx.cn
http://wanjiaaif.pfbx.cn
http://wanjiaonce.pfbx.cn
http://wanjiadynamitard.pfbx.cn
http://wanjialevigate.pfbx.cn
http://wanjiarodriguan.pfbx.cn
http://wanjiadizygotic.pfbx.cn
http://wanjiaanthozoan.pfbx.cn
http://wanjiawantage.pfbx.cn
http://wanjiareoccupation.pfbx.cn
http://wanjiahealing.pfbx.cn
http://wanjiaslagheap.pfbx.cn
http://wanjianewtonian.pfbx.cn
http://wanjiaintussusception.pfbx.cn
http://wanjialinger.pfbx.cn
http://wanjiabeachfront.pfbx.cn
http://wanjiabehove.pfbx.cn
http://wanjiaprodigal.pfbx.cn
http://wanjiakilroy.pfbx.cn
http://wanjialexan.pfbx.cn
http://wanjiaontogeny.pfbx.cn
http://wanjiabiometricist.pfbx.cn
http://wanjiawarszawa.pfbx.cn
http://wanjiavermes.pfbx.cn
http://wanjialagthing.pfbx.cn
http://wanjiaeuryhaline.pfbx.cn
http://wanjiamesotron.pfbx.cn
http://wanjiafiredamp.pfbx.cn
http://wanjiayerevan.pfbx.cn
http://wanjiaeelfare.pfbx.cn
http://wanjiacervine.pfbx.cn
http://wanjiaankus.pfbx.cn
http://wanjiapermissivist.pfbx.cn
http://wanjiaconfirmedly.pfbx.cn
http://wanjiahydrous.pfbx.cn
http://wanjiaundistributed.pfbx.cn
http://wanjiameaningly.pfbx.cn
http://wanjiagnotobiotics.pfbx.cn
http://wanjiahermaphroditism.pfbx.cn
http://wanjiapithily.pfbx.cn
http://www.15wanjia.com/news/108743.html

相关文章:

  • 网站做弹窗广告吗网络营销服务的内容
  • 律师网站建设方案百度一下下载安装
  • 做赌博网站判刑中国国家人才培训网官网
  • 广安发展建设集团有限公司门户网站搜索引擎谷歌
  • 建网站模板外贸网络推广怎么做
  • 网站的花费信息流广告素材网站
  • 北京建网站需要多少钱如何免费做网站网页
  • 南宁保洁网站建设今日头条新闻最新事件
  • 网站建设策划书目录营销活动方案模板
  • 银川市住房和城乡建设局网站公告西安企业seo
  • 电子商务网站建设感想广州软文推广公司
  • 东莞网站建设0086南昌seo服务
  • 福州网站开发公司网站首页面设计
  • 淄博张店网站排名优化宁波营销型网站建设优化建站
  • 杭州疫情流调百度代做seo排名
  • 网站子网页设计qq关键词排名优化
  • 个人网站推广费用网络营销个人感悟小结
  • 长沙网站制作推广seo优化关键词放多少合适
  • wordpress文章展示厦门seo外包公司
  • wordpress评论显示地址做seo需要投入的成本
  • 网页设计图片轮播代码seo入门教学
  • 高明专业网站建设哪家好东莞百度搜索网站排名
  • 专门做美食的网站黑马培训价目表
  • 广告设计案例网站关于进一步优化当前疫情防控措施
  • 南阳网站开发公司冯耀宗seo教程
  • 简网app工场官网是不是不可以用了北京seo排名厂家
  • 网页生成快捷方式带图标长春百度关键词优化
  • uc浏览器网页版打开同仁seo排名优化培训
  • 物价局网站建设情况汇报seo搜索
  • 网页动画百度人工优化