自定义 UITableViewCell 纯代码方式和xib方式

一、纯代码方式:

1、初始化UITableView

#import "TemporaryPayListViewController.h"
#import "TemporaryPayTableViewCell.h"
#import "HomePageOrderModel.h"

@interface TemporaryPayListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong, readwrite) UITableView *tableView;
@property (nonatomic, copy) NSMutableArray *dataArray;
@end

@implementation TemporaryPayListViewController
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        NSMutableArray *mArray = [NSMutableArray array];
        for (int i = 0; i < 4; i++) {
            HomePageOrderModel *homePageOrderModel = [[HomePageOrderModel alloc] initWithData:[NSString stringWithFormat:@"标题%d", i]];
            homePageOrderModel.amount=[NSString stringWithFormat:@"%d", i];
            [mArray addObject:homePageOrderModel];
        }
        _dataArray = mArray;
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitleViewWithTitle:self.title isShowLeftImage:YES];
    
    [self initTableView];
}
NSString *ID=@"TemporaryPayTableViewCell";
-(void) initTableView{
    self.tableView=[[UITableView alloc]init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([self mNavigationbarHeight]);
        make.bottom.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
    }];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[TemporaryPayTableViewCell class] forCellReuseIdentifier:ID];
    self.tableView.separatorColor = [UIColor whiteColor];
    //self.tableView.rowHeight = 140;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //设置tableView所有的cell的真实高度是自动计算的(根据设置的约束)
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    //设置tableView的估算高度
    self.tableView.estimatedRowHeight = 200;
    //设置是否有弹簧效果
    [self.tableView setBounces:NO];
    //当页没有显示完 清除多余的线
//    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
}
  //点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TemporaryPayTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
    
    // 传递数组模型
    cell.model = self.dataArray[indexPath.row];
    //设置选中样式颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end

 

2、、自定义UITableViewCell

    .h

@class HomePageOrderModel;
@interface TemporaryPayTableViewCell : UITableViewCell
@property (nonatomic, strong) HomePageOrderModel *model;
@end

 .m

#import "TemporaryPayTableViewCell.h"

@interface  TemporaryPayTableViewCell ()
@property (nonatomic,weak) UIImageView *image_head;

@end
@implementation TemporaryPayTableViewCell
#pragma mark --------- 添加子控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        UIImageView *image_head=[[UIImageView alloc]init];
        self.image_head=image_head;
       [self.contentView addSubview:self.image_head];
    }
    return self;
}
#pragma mark --------- 设置控件位置
- (void)layoutSubviews{
    [self.image_head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(10);
        make.left.mas_equalTo(0);
        make.width.height.mas_equalTo(20);
    }];
    self.image_head.image=[UIImage imageNamed:@"loginHeadImg"];
    
}
- (void)setModel:(HomePageOrderModel *)model {
    _model = model;
    //赋值数据
//    self.lb_car_color.text =  model.title;
}
@end

 

3、HomePageOrderModel

   .h

@interface HomePageOrderModel : NSObject

@property (nonatomic, copy, readwrite) NSString *uniquekey;
@property (nonatomic, copy, readwrite) NSString *title;
@property (nonatomic, copy, readwrite) NSString *date;
@property (nonatomic, copy, readwrite) NSString *category;
@property (nonatomic, copy, readwrite) NSString *author_name;
@property (nonatomic, copy, readwrite) NSString *url;
@property (nonatomic, copy, readwrite) NSString *amount;
@property (nonatomic, copy, readwrite) NSString *thumbnail_pic_s;
@property (nonatomic, copy, readwrite) NSString *thumbnail_pic_s02;
@property (nonatomic, copy, readwrite) NSString *thumbnail_pic_s03;
@property (assign, nonatomic, getter = isSelect) BOOL isSelect;

- (instancetype)initWithPageData:(NSDictionary *)dict;

- (instancetype)initWithData:(NSString *)title;
- (instancetype)initWithDataIsShow:(NSInteger)isShow;
@end

 

.m

#import "HomePageOrderModel.h"

@implementation HomePageOrderModel
- (instancetype)initWithPageData:(NSDictionary *)dict {
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

- (instancetype)initWithData:(NSString *)title {
    self = [super init];
    if (self) {
        self.title = title;
    }
    return self;
}

@end

 

 

 

 

二、Xib方式

 

posted @ 2021-09-07 12:35  张亚楠  阅读(186)  评论(0编辑  收藏  举报