TableView設(shè)置多種cell(純代碼)

UITableView設(shè)置多種cell
使用繼承方式,cell繼承自baseball,實體繼承自baseEntity

ViewController.m

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

    //獲取數(shù)據(jù)
    [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;
    //設(shè)置tableview自適應(yīng)高度
    self.tableView.estimatedRowHeight = 100;//給定預(yù)估計高度
    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
//指定表視圖有幾個分區(qū)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//指定每個表視圖有幾行數(shù)據(jù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}
//指定每行顯示數(shù)據(jù)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //首先獲取當(dāng)前需要展示的數(shù)據(jù)對象
    BasicNews *news = self.items[indexPath.row];

    //創(chuàng)建cell
    BasicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([news class]) forIndexPath:indexPath];

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

BaseCell.h

#import <UIKit/UIKit.h>
#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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,618評論 30 472
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,271評論 4 61
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,297評論 3 38
  • 我們在上一篇《通過代碼自定義不等高cell》中學(xué)習(xí)了tableView的相關(guān)知識,本文將在上文的基礎(chǔ)上,利用sto...
    啊世ka閱讀 1,649評論 2 7
  • 前幾周就有朋友推薦這部紀(jì)錄片,由真人真事記錄編輯而成的影片。劇中收養(yǎng)人李薇漪對于小狼格林愛的表達(dá)方...
    雷亞榮閱讀 609評論 0 1

友情鏈接更多精彩內(nèi)容