TableView设置多种cell(纯代码)

UITableView设置多种cell
使用继承方式,cell继承自baseball,实体继承自baseEntity

ViewController.m

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.manager = [NewsManager shareNewsManager];

    //获取数据
    [self.manager getDataWithCompletionHandler:^(NSArray *arr) {
        self.items = [NSArray arrayWithArray:arr];
    }];
    //初始化tableview
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
//    self.tableView.rowHeight = 100;
    //设置tableview自适应高度
    self.tableView.estimatedRowHeight = 100;//给定预估计高度
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //绑定cell
    [self.tableView registerClass:[WritingCell class]forCellReuseIdentifier:NSStringFromClass([WritingNews class])];
    [self.tableView registerClass:[PictureCell class] forCellReuseIdentifier:NSStringFromClass([PictureNews class])];
}
#pragma mark tableview data source Method
//指定表视图有几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//指定每个表视图有几行数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}
//指定每行显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //首先获取当前需要展示的数据对象
    BasicNews *news = self.items[indexPath.row];

    //创建cell
    BasicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([news class]) forIndexPath:indexPath];

    //配置cell
    [cell setCellInfo:news];
    return cell;
}

BaseCell.h

#import 
#import "BasicNews.h"

@interface BasicCell : UITableViewCell

//只是声明一个单元格赋值的方法
-(void)setCellInfo:(BasicNews *)news;

@end

WritingCell继承BaseCell .m中如下

#import "WritingCell.h"
#import "WritingNews.h"

@interface WritingCell ()

@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *subTitleLabel;
@property (nonatomic,strong)UIImageView *MyImageView;

@end

@implementation WritingCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //组件初始化
        self.MyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(8, 6, 60, 48)];
        [self.contentView addSubview:self.MyImageView];
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(72, 6, 200, 24)];
        self.titleLabel.font = [UIFont systemFontOfSize:25];
        [self.contentView addSubview:self.titleLabel];
        self.subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(72, 32, 200, 26)];
        self.subTitleLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.subTitleLabel];
    }
    return self;
}

-(void)setCellInfo:(WritingNews *)news{
    self.titleLabel.text = news.title;
    self.subTitleLabel.text = news.subTitle;
    self.MyImageView.image = news.headImage;
}

PictureCell继承自BaseCell .m如下

#import "PictureCell.h"
#import "PictureNews.h"

@interface PictureCell ()

@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIImageView *imageView1;
@property (nonatomic,strong)UIImageView *imageView2;
@property (nonatomic,strong)UIImageView *imageView3;

@end

@implementation PictureCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 8, 300, 20)];
        self.titleLabel.font = [UIFont systemFontOfSize:20];
        [self.contentView addSubview:self.titleLabel];
        //添加约束
        self.imageView1 = [[UIImageView alloc]initWithFrame:CGRectZero];
        [self.contentView addSubview:self.imageView1];
        self.imageView1.translatesAutoresizingMaskIntoConstraints = NO;
        NSArray *Height1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-28-[_imageView1(60)]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1)];
        NSArray *Width1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[_imageView1(96)]-208-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1)];
        [self.contentView addConstraints:Height1];
        [self.contentView addConstraints:Width1];
        
        self.imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(112, 28, 96, 30)];
        [self.contentView addSubview:self.imageView2];
        self.imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(208, 28, 96, 30)];
        [self.contentView addSubview:self.imageView3];
    }
    return self;
}
-(void)setCellInfo:(PictureNews *)news{
    self.titleLabel.text = news.title;
    self.imageView1.image = news.image1;
    self.imageView2.image = news.image2;
    self.imageView3.image = news.image3;
}

你可能感兴趣的:(TableView设置多种cell(纯代码))