32.自定义tableViewCell

本文介绍了如何创建自定义UITableViewCell子类,通过封装控件和设置Model属性来简化UIViewController中的代码。讲解了Model、View和Controller之间的通信,并列举了UIView的几个关键方法,如layoutSubviews和didMoveToSuperview。同时,提到了自定义cell的实现文件,如Student Model和两个自定义Cell的视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 自定义cell就是创建一个UITableViewCell的子类。把cell上的控件创建都封装在子类中,简化UIViewController中的代码子视图控件添加到cell的contentView上。
  2. cell中声明一个Model类型的属性,viewController中获取到Model对象后赋值给cell的Model属性,cell中重写Model的setter方法,把Model对象中的内容重新赋值给各个控件,M和V不直接进行通信,C负责M和V之间进行通信.
  3. UIView自带的方法
    • 1> - (void)layoutSubviews;
    • 当一个控件的frame发生改变的时候就会自动调用
    • 一般在这里布局内部的子控件(设置子控件的frame)
    • 一定要调用super的layoutSubviews方法
    • 2> - (void)didMoveToSuperview;
    • 当一个控件被添加到父控件中就会调用
      3> - (void)willMoveToSuperview:(UIView *)newSuperview;
    • 当一个控件即将被添加到父控件中会调用

1.一个(Student)Model

Student.h和Student.m文件

#import <Foundation/Foundation.h>
@interface Student : NSObject
//四条和字典对应的属性
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *phone;
@property(nonatomic,copy)NSString *hobby;
@end
#import "Student.h"
@implementation Student
- (void)dealloc
{
    [_hobby release];
    [_name release];
    [_sex release];
    [_phone release];
    [super dealloc];
}
//如果使用KVC,这个方法一定要写,避免了应为key和属性名不匹配造成不必要的崩溃
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end

2.两对自定义cell的view

MyCell.h和MyCell.m文件

@interface MyCell : UITableViewCell
#warning 现在要给自定义的cell加上四条属性,而且需要在外部进行赋值,所以在.h写属性的声明,而且者四个属性,他们的名不能和系统已有的属性名重复,包括imageView,textLabel,detailTextLabel
@property(nonatomic,retain)UIImageView *leftImageView;
@property(nonatomic,retain)UILabel *upLabel;
@property(nonatomic,retain)UILabel *downLabel;
@property(nonatomic,retain)UIImageView *rightImageView;
@end
#import "MyCell.h"
#define  WIDTH self.contentView.frame.size.width
#define  HEIGHT self.contentView.frame.size.height

@implementation MyCell
#pragma mark 重写cell的初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //完成对属性视图的创建,但是一般创建好后不给属性视图frame
        [self createView];
    }
    return self;
}
#pragma mark 属性视图进行创建
- (void)createView{
    //创建左imageView
    self.leftImageView = [[UIImageView alloc] init];
    self.leftImageView.backgroundColor = [UIColor greenColor];
    //cell上有一个专门用来显控件的视图contentView,我们把视图就加载到上面
    [self.contentView addSubview:self.leftImageView];
    [_leftImageView release];

    self.upLabel = [[UILabel alloc] init];
    self.upLabel.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.upLabel];
    [_upLabel release];

    self.downLabel = [[UILabel alloc] init];
    self.downLabel.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.downLabel];
    [_downLabel release];

    self.rightImageView = [[UIImageView alloc] init];
    self.rightImageView.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.rightImageView];
    [_rightImageView release];
}

#pragma mark 这个方法是cell显示之前走的的左后的一个方法,一般会在这个方法里设置所有的属性视图的大小和尺寸,这个方法会用在图片文字的自适应的设置上
- (void)layoutSubviews{
    //重写了父类的layoutSubviews方法,吐过想哟啊这个方法发挥正常功能,不能少了[super layoutSubviews];
    [super layoutSubviews];
    //对所有属性视图的位置和大小设置
    self.leftImageView.frame = CGRectMake(0, 0,WIDTH/3,HEIGHT);
    self.upLabel.frame = CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT/2);
    self.downLabel.frame = CGRectMake(WIDTH/3, HEIGHT/2, WIDTH/3, HEIGHT/2);
    self.rightImageView.frame = CGRectMake(WIDTH/3*2, 0, WIDTH/3, HEIGHT);
}
- (void)dealloc
{
    [_rightImageView release];
    [_leftImageView release];
    [_upLabel release];
    [_downLabel release];
    [super dealloc];
}

MyOwnCell.h和MyOwnCell.m文件

#import <UIKit/UIKit.h>
@interface MyOwnCell : UITableViewCell

@property(nonatomic,retain)UIImageView *leftImageView;
@property(nonatomic,retain)UIImageView *midImageView;
@property(nonatomic,retain)UIImageView *rightImageView;
@property(nonatomic,retain)UILabel *leftLabel;
@property(nonatomic,retain)UILabel *rightLabel;
@end
#import "MyOwnCell.h"
#define  WIDTH self.contentView.frame.size.width
#define  HEIGHT self.contentView.frame.size.height
@implementation MyOwnCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView{
    self.leftImageView = [[UIImageView alloc] init];
    self.leftImageView.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:self.leftImageView];
    [_leftImageView release];

    self.midImageView = [[UIImageView alloc] init];
    self.midImageView.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.midImageView];
    [_midImageView release];

    self.rightImageView = [[UIImageView alloc] init];
    self.rightImageView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.rightImageView];
    [_rightImageView release];

    self.leftLabel = [[UILabel alloc] init];
    self.leftLabel.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.leftLabel];
    [_leftLabel release];

    self.rightLabel = [[UILabel alloc] init];
    self.rightLabel.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview: self.rightLabel];
    [_rightLabel release];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.leftImageView.frame = CGRectMake(0, 0, WIDTH/3, HEIGHT*2/3);
    self.midImageView.frame = CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT*2/3);
    self.rightImageView.frame = CGRectMake(WIDTH*2/3, 0, WIDTH/3, HEIGHT*2/3);
    self.leftLabel.frame = CGRectMake(0, HEIGHT*2/3, WIDTH/2, HEIGHT/3);
    self.rightLabel.frame = CGRectMake(WIDTH/2, HEIGHT*2/3, WIDTH/2, HEIGHT/3);
}
- (void)dealloc
{
    [_rightLabel release];
    [_leftLabel release];
    [_leftImageView release];
    [_midImageView release];
    [_rightImageView release];
    [super dealloc];
}

MainViewController.m文件

#import "MainViewController.h"
#import "MyCell.h"
#import "MyOwnCell.h"
#import "Student.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *studentArr;
@end
@implementation MainViewController
- (void)dealloc
{
    [_tableView release];
    [_studentArr release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
    }
    return self;
}

- (void)createData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentArr.plist" ofType:nil];
    NSArray *stuArr = [NSArray arrayWithContentsOfFile:path];
    //通过KVC对model进行赋值
    self.studentArr = [NSMutableArray array];
    for (NSDictionary *stuDic in stuArr) {
        Student *stu = [[Student alloc] init];
        [stu setValuesForKeysWithDictionary:stuDic];
        [self.studentArr addObject:stu];
        [stu release];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"表视图";
    self.navigationController.navigationBar.translucent = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 603) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.rowHeight = 180;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.studentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Student *stu = self.studentArr[indexPath.row];
    if (indexPath.row % 2 == 1) {
        static NSString *ID = @"myCell";
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.upLabel.text = stu.name;
        cell.downLabel.text = stu.phone;
        cell.leftImageView.image = [UIImage imageNamed:@"4.jpg"];
        cell.rightImageView.image = [UIImage imageNamed:@"5.jpg"];
        return cell;
    }
    else{
        static NSString *ID = @"MyOwnCell";
        MyOwnCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[MyOwnCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.leftLabel.text = stu.name;
        cell.rightLabel.text = stu.phone;
        cell.leftImageView.image = [UIImage imageNamed:@"9.jpg"];
        cell.midImageView.image = [UIImage imageNamed:@"6.jpg"];
        cell.rightImageView.image = [UIImage imageNamed:@"10.jpg"];
        return cell;
    }
    return nil;
}

简单截图
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值