自定义TCELL

本文详细介绍了一个自定义UITableViewCell的过程,包括创建自定义单元格类JTableViewCell,实现UITableViewDataSource和UITableViewDelegate方法,以及如何在TableViewContain视图控制器中使用自定义单元格。

自定义TCELL

//
//  TableViewContain.m
//  ChangeLocation
//
//  Created by Kevin Jin on 2020/9/8.
//  Copyright © 2020 ljb48229. All rights reserved.
//

#import "TableViewContain.h"
#import "JTableViewCell.h"

static const CGFloat MBDefaultDetailsLabelFontSize = 12.f;


@interface TableViewContain()<UITableViewDelegate,UITableViewDataSource>
 
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation TableViewContain


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self setUpView];
    }
    return self;
}

- (void)setUpView
{
    [self addSubview:self.tableView];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [UIView new];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JTableViewCell *cell = [JTableViewCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,350, 500) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = [UIView new];
        _tableView.tableFooterView = [UIView new];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}
@end

//
//  JTableViewCell.m
//  ChangeLocation
//
//  Created by Kevin Jin on 2020/9/8.
//  Copyright © 2020 ljb48229. All rights reserved.
//

#import "JTableViewCell.h"

@interface JTableViewCell()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *keyTxt;

@property (nonatomic, strong) UITextField *valueTxt;

@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation JTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"JTableViewCell";
    // 1.缓存中取
    JTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[JTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.contentView addSubview:self.keyTxt];
        [self.contentView addSubview:self.valueTxt];
        [self.contentView addSubview:self.deleteBtn];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

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

    // Configure the view for the selected state
}

- (void)submitClickAction:(UIButton *)button{
    
}


#pragma mark - get
- (UIButton *)deleteBtn
{
    if (!_deleteBtn) {
        _deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width - 20, 10, 50, self.contentView.frame.size.height - 20)];
        _deleteBtn.backgroundColor = UIColor.blueColor;
        _deleteBtn.layer.cornerRadius = 5;
        [_deleteBtn addTarget:self action:@selector(submitClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _deleteBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    }
    return _deleteBtn;
}

- (UITextField *)keyTxt
{
    if (!_keyTxt) {
        _keyTxt = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, 200, 30)];
        _keyTxt.font = [UIFont systemFontOfSize:15];
        _keyTxt.layer.borderWidth = 0.5;
        _keyTxt.layer.cornerRadius = 10;
        _keyTxt.layer.masksToBounds = YES;
        _keyTxt.layer.borderColor = UIColor.grayColor.CGColor;
        _keyTxt.textColor = UIColor.blackColor;
        _keyTxt.placeholder = @"买家留言";
        CGRect frame = [_keyTxt frame];
        frame.size.width = 7.0f;
        UIView *leftview = [[UIView alloc] initWithFrame:frame];
        _keyTxt.leftViewMode = UITextFieldViewModeAlways;
        _keyTxt.returnKeyType = UIReturnKeyDone;
//        [_keyTxt setInputAccessoryView:self.toolBar];
        _keyTxt.leftView = leftview;
        _keyTxt.delegate = self;
    }
    return _keyTxt;
}

- (UITextField *)valueTxt
{
    if (!_valueTxt) {
        _valueTxt = [[UITextField alloc] initWithFrame:CGRectMake(10, 45, 200, 30)];
        _valueTxt.font = [UIFont systemFontOfSize:15];
        _valueTxt.layer.borderWidth = 0.5;
        _valueTxt.layer.cornerRadius = 10;
        _valueTxt.layer.masksToBounds = YES;
        _valueTxt.layer.borderColor = UIColor.grayColor.CGColor;
        _valueTxt.textColor = UIColor.blackColor;
        _valueTxt.placeholder = @"买家留言";
        CGRect frame = [_valueTxt frame];
        frame.size.width = 7.0f;
        UIView *leftview = [[UIView alloc] initWithFrame:frame];
        _valueTxt.leftViewMode = UITextFieldViewModeAlways;
        _valueTxt.returnKeyType = UIReturnKeyDone;
//        [_valueTxt setInputAccessoryView:self.toolBar];
        _valueTxt.leftView = leftview;
        _valueTxt.delegate = self;
    }
    return _valueTxt;
}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值