• 自定义UITableViewCell


    随着日常的使用,系统提供的cell已经不能满足开发的需要,因为系统提供的是单一的,所以 这就引来了自定义cell的出现,可以根据 自己的需要来布局各个控件所处的位置。不同位置显示不同的控件。

    创建一个类,继承于UITableCell.

    自定义cell,简单的来说可以分为三步

    1.将所有cell要显示的子视图控件声明成属性

    @interface MyTableViewCell : UITableViewCell
    
    @property (nonatomic, retain)UIImageView *headerImageView;  //头像
    @property (nonatomic, retain)UILabel *nameLabel;   // 姓名
    @property (nonatomic, retain)UILabel *genderLabel;  // 性别
    @property (nonatomic, retain)UILabel *ageLabel;   //年龄

    2.重写cell的初始化方法,frame给定为0,将控件添加到cell上面进行显示,一定要注意使用self.contentView添加

    //CGRectZero表示0

    //重写cell的初始化方法
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            [self setupSubviews];
        }
        return self;
    }
    - (void)setupSubviews{
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _headerImageView.backgroundColor = [UIColor greenColor];
        //自定义cell内部添加子视图时,不能使用self,应该是使用self.contentView
        [self.contentView addSubview:_headerImageView];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _nameLabel.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_nameLabel];
        
        _genderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _genderLabel.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_genderLabel];
        
        _ageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _ageLabel.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_ageLabel];
    }

    3.重写layouSubviews方法,给定内部控件的具体位置

    //指定内部控件的大小
    - (void)layoutSubviews{
        [super layoutSubviews];
        _headerImageView.frame = CGRectMake(5, 5, 50, 80);
        _nameLabel.frame = CGRectMake(65, 5, 150, 20);
        _genderLabel.frame = CGRectMake(65, 35, 150, 20);
        _ageLabel.frame = CGRectMake(65, 65, 150, 20);
    }

    但如果想方便以后操作的话,可以在自定义cell的时候,使用到数据模型,方便赋值,取值。

    1.导入模型,将模型与cell进行绑定,声明模型属性

    //在cell内部绑定一个模型属性
    @property (nonatomic, retain)Student *stu;

    2.重写模型属性的setter方法,内部使用模型为内部控件赋值。

    //重写模型的setter方法,完成赋值
    - (void)setStu:(Student *)stu{
        if (_stu != stu) {
            _stu = [stu retain];
            //为内部控件进行赋值
            _headerImageView.image = [UIImage imageNamed:_stu.picture];
            _nameLabel.text = _stu.name;
            _genderLabel.text = _stu.gender;
            _ageLabel.text = _stu.age;
        }
    }

    如果是在MRC环境下使用的话,一定要注意内存管理哦。

    自定义cell的使用方法与系统提供的cell使用方法,没有区别。

    //cell显示的内容,数据
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *reuseIdentifier = @"reuse";
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]autorelease];
            //cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    //使用模型属性来赋值
        Student *student = _dataArray[indexPath.row];
        cell.stu = student;
    //    cell.nameLabel.text = student.name;
    //    cell.ageLabel.text = student.age;
    //    cell.genderLabel.text = student.gender;    
        return cell;
    }

     自定义cell,一个重点就是内部各控件该怎么样布局,控件显示的属性有哪些、而怎么样局面,摆放的位置还是要自己设计清楚的。

  • 相关阅读:
    设计模式-14-桥接模式
    设计模式-13-中介者模式
    设计模式-12-命令模式
    设计模式-11-外观模式
    设计模式-10-装饰器
    设计模式-9-模板
    设计模式-8-适配器模式-组合
    设计模式-8-适配器模式-继承
    设计模式-7-策略模式+工厂模式
    设计模式-7-策略模式
  • 原文地址:https://www.cnblogs.com/xueyao/p/5186236.html
Copyright © 2020-2023  润新知