zoukankan      html  css  js  c++  java
  • 关于tableView的一些优化

    关于优化
    除了之前用单例模式把数据放在一个类中的优化之外,今天又总结了其他几种关于tableView的一些优化
    1.label等控件
    当label的公共样式属性很多的时候,我们有需要建很多label的时候,我们可以创建一个categary 把公共属性部分放在方法中 把不同的属性作为参数传进去
    2.cell内容显示
    cell显示内容的时候,之前我们是对cell中的每个控件依次赋值,如果这个自定义cell中有几十个控件的话,我们需要写几十行代码,显然这太繁琐了
    优化前:
            cell.iconImageViwe.image = [UIImage imageNamed:stu.icon];
            cell.
    nameLabel.text = stu.name;
            cell.
    sexLable.text = stu.sex;
            cell.
    phoneNumberLable.text = stu.phoneNumber;
            cell.
    introduceLable.text = stu.introduce;
     
    优化后: cell.student = stu;
     
    name、sex、phoneNumber等这些都是学生对象的属性,所以我们可以把这个学生直接传给cell,
    cell拿到这个学生对象以后,再把这个学生对象的各个属性赋给自己的子控件。
    cell要能直接拿到这个学生对象,就需要有一个student的属性可供外界访问和赋值。然后我们需要重写这个属性的setter方法。
    -(void)setStudent:(Student *)student
    {
        
    if (_student != student) {
            [
    _student release];
            
    _student = [student retain];
        }
        // 拿到这个对象以后把对象的属性赋给cell的各个子控件,那么子控件上显示相应的值
        
     self.iconImageViwe.image = [UIImage imageNamed:student.icon];
        
    self.nameLabel.text = student.name;
        
    self.phoneNumberLable.text = student.phoneNumber;
        
    self.sexLable.text = student.sex;
        
    self.introduceLable.text = student.introduce;
        
    }
    3.把自定义cell中的视图设为懒加载对象(该视图需要被使用的时候才会被创建)
    该对象只有在被取值,即被调用它的getter方法时,对象才会被创建,所以其初始化是在getter方法中实现的,所以我们需要改写每个属性的getter方法,先判断该对象是否为空,若为空,则创建。例如:
    -(UILabel *)nameLabel
    {
        
    if (_nameLabel == nil) {
            
    _nameLabel = [[UILabel allocinitWithFrame:CGRectMake(CGRectGetMaxX(_iconImageViwe.frame) + kMargin_iconImageViwe.frame.origin.y15030)];
            
    _nameLabel.backgroundColor = [UIColor cyanColor];
            [
    self addSubview:_nameLabel];
        }
        
    return _nameLabel;
    }
  • 相关阅读:
    PHP数组(数组正则表达式、数组、预定义数组)
    面向对象。OOP三大特征:封装,继承,多态。 这个讲的是【封存】
    uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并
    LA4329 Ping pong 树状数组
    HDU 1257 最少拦截系统
    HDU 1260 Tickets
    codeforce 621D
    codeforce 621C Wet Shark and Flowers
    codeforce 621B Wet Shark and Bishops
    codeforce 621A Wet Shark and Odd and Even
  • 原文地址:https://www.cnblogs.com/liuyu521/p/3757620.html
Copyright © 2011-2022 走看看