自定义TableView 继承, cell, model

准备工作不赘述
MainViewController.m

#import "MainViewController.h"
#import "MyCell.h"
#import "MyCell2.h"
#import "Student.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSArray *arr;
@property(nonatomic,retain)NSArray *picArr;





@end

@implementation MainViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
        self.picArr=[NSArray alloc];
        for (NSInteger i=1; i<15; i++) {
            self.picArr=[[NSArray alloc]initWithObjects:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",i]], nil];
        }

        [self createData];

    }
    return self;
}


-(void)createData
{
    NSString *path=[[NSBundle mainBundle]pathForResource:@"Student" ofType:@"plist"];
    NSArray *stuArr=[NSArray arrayWithContentsOfFile:path];

    NSDictionary *dic=stuArr[0];
    // 通过kvc对model进行赋值
    Student *stu=[[Student alloc]init];
    [stu setValuesForKeysWithDictionary:dic];
    NSLog(@"%@",stu.name);


}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent=NO;

    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    self.tableView.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    [self.tableView release];

    self.tableView.rowHeight=160;




}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2==1) {
        static NSString *reuse=@"reuse";
        MyCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell=[[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
            cell.upLabel.text=self.arr[indexPath.row];
        cell.leftImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", self.picArr[indexPath.row +1]]];
            cell.rightImageView.image=[UIImage imageNamed:@"7.jpg"];

//            cell.leftImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]];


        return cell;

    }
    else{
        static NSString *reuse=@"newReuse";
        MyCell2 *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell=[[[MyCell2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }

        cell.leftImageView.image=[UIImage imageNamed:@"8.jpg"];
        cell.rightImageView.image=[UIImage imageNamed:@"6.jpg"];
        cell.leftLabel.text =@"limuran";
        return cell;
    }

}

Student.h

#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

Student.m

#import "Student.h"

@implementation Student
-(void)dealloc
{
    [_name release];
    [_sex release];
    [_hobby release];
    [_phone release];
    [super dealloc];
}


// 没有找到相匹配的key的话就会走这个方法,相当于那个属性key所对应的那个value是空的
// 如果是用kvc,这个方法一定要写,避免了因为key和属性名不匹配造成不必要的崩溃
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    // 里面可以什么都不用写
    // 也可以加判断
//    if(key isEqualToString:@"sex"){
//        
//    }
}

MyCell2.h

#import <UIKit/UIKit.h>

@interface MyCell2 : UITableViewCell
@property(nonatomic,retain)UIImageView *leftImageView;
@property(nonatomic,retain)UIImageView *centerImageView;
@property(nonatomic,retain)UIImageView *rightImageView;
@property(nonatomic,retain)UILabel *leftLabel;
@property(nonatomic,retain)UILabel *rightLabel;
@end

MyCell2.m

#import "MyCell2.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@implementation MyCell2
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createImageView];
    }
    return self;
}

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

    self.centerImageView=[[UIImageView alloc]init];
    self.centerImageView.backgroundColor=[UIColor orangeColor];
    [self.contentView addSubview:self.centerImageView];
    [self.centerImageView release];


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


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


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

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.leftImageView.frame=CGRectMake(10, 0, 70, HEIGHT);

    self.centerImageView.frame=CGRectMake(100, 10, 70, 70);

    self.rightImageView.frame=CGRectMake(210, 10, 70, 70);

    self.leftLabel.frame=CGRectMake(20, 90, 100, 30);

    self.rightLabel.frame=CGRectMake(150, 90, 100, 30);

}

-(void)dealloc
{
    [_leftImageView release];
    [_rightImageView release];
    [_centerImageView release];
    [_leftLabel release];
    [_rightLabel release];
    [super dealloc];
}

MyCell.h

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

#warning 现在要给自定义的cell加上4条属性,而且需要在外部进行赋值,所以在.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

MyCell.m

#import "MyCell.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@implementation MyCell

#pragma mark 重写cell的初始化方法


// 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 redColor];

    // 添加
    // cell有一个专门用来显示控件的视图,叫contentView,我们把视图就放到contentView上进行显示
    [self.contentView addSubview:self.leftImageView];
    [_leftImageView release];


    // 创建右imageView
    self.rightImageView=[[UIImageView alloc]init];
    self.rightImageView.backgroundColor=[UIColor yellowColor];
    [self.contentView addSubview:self.rightImageView];
    [_rightImageView release];


    // 创建上label
    self.upLabel=[[UILabel alloc]init];
    self.upLabel.layer.borderWidth=1;
    [self.contentView addSubview:self.upLabel];
    [self.upLabel release];


    // 创建下label
    self.downLabel=[[UILabel alloc]init];
    self.downLabel.layer.borderWidth=1;
    [self.contentView addSubview:self.downLabel];
    [self.downLabel release];


}


#pragma mark 这个方法是cell显示之前走的最后一个方法,一般会在这个方法里设置所有的属性视图的大小
-(void)layoutSubviews
{
    // 重写了父类的layoutSubviews方法,如果想要这个方法发挥正常功能,别玩了[super layoutSubviews]
    [super layoutSubviews];

    // 对所有属性视图的位置和大小设置
    self.leftImageView.frame=CGRectMake(0, 0, WIDTH/3, HEIGHT);

    // 对upLabel进行设置
    self.upLabel.frame=CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT/2);

    // 对downLabel进行设置
    self.downLabel.frame=CGRectMake(WIDTH/3, HEIGHT/2, WIDTH/3, HEIGHT/2);
    self.downLabel.backgroundColor=[UIColor blueColor];

    self.rightImageView.frame=CGRectMake(WIDTH/3 *2, 0, WIDTH/3, HEIGHT); 
}

-(void)dealloc
{
    [_leftImageView release];
    [_rightImageView release];
    [_upLabel release];
    [_downLabel release];
    [super dealloc];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值