UITableView性能检测相关

图片名称

今天目的就是对这个页面进行性能优化

使用YYFPSLabel 进行FPS检测基本维持在59~60之间,但是滑动的时候卡顿还是挺明显的,之后用Instruments进行检测,其实只有50左右

1. 关于cell相关处理说明
  • 采用的注册Nib方法
  • [tableView_registerNib:[UINib nibWithNibName:@"WHBKNeighborMyGrabsTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([WHBKNeighborMyGrabsTableViewCell class])];
  • 获取方法是用
  • WHBKNeighborMyGrabsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHBKNeighborMyGrabsTableViewCell class]) forIndexPath:indexPath];
2. 内容说明

图片名称

    如图所示,是一个基本cell样本,行高提前缓存好了,红色框中是一个设置了NSMutableParagraphStyle的Label1,高度不固定,依靠拉伸Label高度的Constant值来修改,值也缓存好了,第二行也是动态高度Label2,依赖设定cell高度来改变
3. 进行优化
优化之前设置cell数据是在setModel中去设置,并且在setModel中对数据进行整合,代码如下:

_model = model;
_descLabel.text = [WHBKReleaseTaskTool textPack:model.taskContent];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineSpacing;
_descLabel.attributedText = [[NSAttributedString alloc] initWithString:[WHBKReleaseTaskTool textPack:model.taskContent] attributes:@{NSParagraphStyleAttributeName:[paragraphStyle copy],NSFontAttributeName : [UIFont systemFontOfSize:fontSize] }];
NSString *str = [Helper convertToDateWith:[model.acctDate stringByAppendingString:model.pubTime] separateStr:@"-" isChinese:NO] ;
str = [str substringToIndex:16];
_dateLabel.text = str;
_rewardLabel.attributedText = nil;
_rewardLabel.text = [@"奖励: " stringByAppendingString:model.rewardDesc];
[ZYTextAttribute properFont:_rewardLabel.font.pointSize+3 withView:_rewardLabel range:NSMakeRange(4, model.rewardDesc.length)];

  1. 第一次优化: model类提前将所有数据合成好,包括字符拼接,NSAttributeString合成,之后代码如下
    _model = model;
    _descLabel.attributedText = model.descAtt;
    _addressLabel.text = model.detailContactWay;
    _dateLabel.text = model.doneTime;
    _rewardLabel.attributedText = model.awrdAtt;

    移除额外所需要的数据处理步骤,提前交由Model生成好,这时候用Instruments进行检测,在每次重用cell的时候还是顿卡,查看下主要耗时的方法
    耗时
    setModel里面还是占用了大量的时间,当把setModel中的方法注释掉之后瞬间流畅…
4. 对setModel内的赋值进行耗时测试
  • 具体可以看这里
    发现每次复制给NSMutableParagraphStyle的NSAttributeString给Label1耗时很长,并且随着字数\行数增加而增加,平均在6~8ms…
5. 使用纯代码
  • 从注册nib —-> 注册class
  • 纯代码 ,手动更新布局

性能略微有所提升, 但是相对于xib+autolayout 需要花1倍的时间,并且不直观,解决bug时间也增加。一般性能不是要求非常高的tableView建议还是使用注册nib的方式,节省时间…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UITableView的section中添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议中实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格中的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section中的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个section的UITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子中,我们创建了一个包含两个section的UITableView。每个section都有一个字符串数组,我们将其存储在data数组中。在numberOfSections方法中,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法中,我们返回特定section中的行数。最后,在tableView(_:cellForRowAt:)方法中,我们获取特定indexPath的字符串并将其显示在UITableViewCell中。 注意,在上述示例代码中,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件中对应的UITableViewCell的标识符也设置为“Cell”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值