关于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   alloc initWithFrame : CGRectMake ( CGRectGetMaxX ( _iconImageViwe . frame ) +  kMargin _iconImageViwe . frame . origin . y 150 30 )];
        
_nameLabel . backgroundColor  = [ UIColor   cyanColor ];
        [
self   addSubview : _nameLabel ];
    }
    
return   _nameLabel ;}
相关文章
相关标签/搜索