Snail—UI学习之UITableView之自定义UITableViewCell

之前都是用得系统的UItableViewCell 局限性比较大 不够自由

自定义的cell满足了我们所有的需求 以后做项目的时候也是大部分都是用自定义的


后面我们要把数据放在数据模型里 数据源数组里将要存储的是model

BookModel.h

#import <Foundation/Foundation.h>

@interface WJJBookModel : NSObject

@property (nonatomic,copy) NSString * title;

@property (nonatomic,copy) NSString * price;

@property (nonatomic,copy) NSString * icon;

@property (nonatomic,copy) NSString * detail;


@end

BoookModel.m

#import "WJJBookModel.h"

@implementation WJJBookModel

@end


自定义cell  就得写一个Cell的类 继承于UITableViewCell

MyFirstCell.h

#import <UIKit/UIKit.h>
#import "WJJBookModel.h"

@interface WJJMyFirstCell : UITableViewCell


//在cell上面将要显示的imageView
@property (nonatomic,strong) UIImageView * headerView;

@property (nonatomic,strong) UILabel * titleLabel;

@property (nonatomic,strong) UILabel * detailLabel;

@property (nonatomic,strong) UILabel * priceLabel;

//得到一个Model 并且在这个方法里给cell赋值
- (void)configCellWithBook:(WJJBookModel *)book;

@end

MyFirstCell.m

#import "WJJMyFirstCell.h"

@implementation WJJMyFirstCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //给属性实例化
        [self configUI];
    }
    return self;
}

- (void)configUI{
    self.headerView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 70, 70)];
    //把headerView 加到视图上
    [self.contentView addSubview:self.headerView];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 200, 20)];
    self.titleLabel.textColor = [UIColor purpleColor];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.titleLabel];
    
    self.detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 30, 200, 20)];
    self.detailLabel.textColor = [UIColor redColor];
    self.detailLabel.font = [UIFont systemFontOfSize:15];
    self.detailLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.detailLabel];
    
    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 50, 200, 20)];
    self.priceLabel.textColor = [UIColor blueColor];
    self.priceLabel.font = [UIFont systemFontOfSize:15];
    self.priceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.priceLabel];
    
}

//给cell的属性赋值
- (void)configCellWithBook:(WJJBookModel *)book{
    
    [self.headerView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",book.icon]]];
    self.titleLabel.text = book.title;
    self.detailLabel.text = book.detail;
    self.priceLabel.text = book.price;

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

然后在RootViewController.m中写如下代码

#import "WJJRootViewController.h"
#import "WJJMyFirstCell.h"
#import "WJJBookModel.h"

@interface WJJRootViewController (){
    UITableView * _tableView;
    NSMutableArray * _dataArray;
}

@end

@implementation WJJRootViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.title = @"书签";
    [self createDataSource];
}

- (void)createDataSource{
    _dataArray = [[NSMutableArray alloc] init];
    
    NSString * path = [[NSBundle mainBundle]pathForResource:@"bookData" ofType:@"plist"];
    NSArray * rootArray = [[NSArray alloc] initWithContentsOfFile:path];
    
    for (NSDictionary * dict in rootArray) {
        //[_dataArray addObject:dict];
        WJJBookModel * book = [[WJJBookModel alloc] init];
        //[book setValuesForKeysWithDictionary:dict];
        book.icon = [dict objectForKey:@"icon"];
        book.price = [dict objectForKey:@"price"];
        book.title = [dict objectForKey:@"title"];
        book.detail = [dict objectForKey:@"detail"];
        [_dataArray addObject:book];
    }
    NSLog(@"%@",_dataArray);
    [self createTableView];
}

- (void)createTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
}

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WJJMyFirstCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        cell = [[WJJMyFirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    
    WJJBookModel * book = _dataArray[indexPath.row];
    [cell configCellWithBook:book];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值