关于自定义cell

自定义cell 其实就是 重建一个cell 继承UITableViewCell,然后再在tableView的方法里写。

先创建一个cell 类继承UITableViewCell,先看NewCell.h 文件。

#import <UIKit/UIKit.h>

@interface NewCell : UITableViewCell

@property (nonatomic,weak)UIImageView *iconView;
@property (nonatomic,weak)UILabel *labNameLabel;
@property (nonatomic,weak)UILabel *promptLabel;

+(instancetype)cellWithTabelView:(UITableView *)tableView;


@end

定义了几个属性 将是未来Cell 里面的内容,也定义了一个类方法,可以快速创建cell

再看下NewCell.m文件

#import "NewCell.h"

@implementation NewCell

#define NameFont [UIFont systemFontOfSize:15]
#define promptFont [UIFont systemFontOfSize:12];


- (void)awakeFromNib {
    // Initialization code
    
}

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

    // Configure the view for the selected state
}
+(instancetype)cellWithTabelView:(UITableView *)tabelView{
    static NSString *ID = @"cell";
    
    NewCell *cell = [tabelView dequeueReusableCellWithIdentifier:ID];
    if (cell  == nil) {
        cell = [[NewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    return cell;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       UIImageView *iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
<h3>        </h3>        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.font = NameFont;
        [self.contentView addSubview:nameLabel];
        self.labNameLabel = nameLabel;
        
        
        UILabel *prompt = [[UILabel alloc]init];
        prompt.font = promptFont;
        prompt.textColor = [UIColor grayColor];
        [self.contentView addSubview:prompt];
        self.promptLabel = prompt;
        
        [self.contentView setBackgroundColor:[UIColor clearColor]];
        [self settingFrame];
        
    }
    return self;
}
-(void)settingFrame{
    self.frame = CGRectMake(0, 0, 320, 80);
    self.labNameLabel.frame = CGRectMake(100, 80/2-25, 200, 50);
    self.iconView.frame =CGRectMake(10, (120-80)/2, 40, 40);
    self.promptLabel.frame = CGRectMake(240, 80/2 -25, 200, 30);
    
}
@end

NewCell.m中定义了各个控件的位置,以及基本大小,颜色,等等。

再回到ViewController.m中 来创建一个表

#import "ViewController.h"
#import "NewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UITableView *tableViewNew = [[UITableView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:tableViewNew];
    tableViewNew.delegate  = self;
    tableViewNew.dataSource = self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat
{//调用 自定义的tableViewCell
    NewCell *cell=[NewCell cellWithTabelView:tableView];
    cell.labNameLabel.text=@"aaaa";
    cell.iconView.image=[UIImage imageNamed:@"123.png"];
    cell.promptLabel.text =@"bbbb";
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
}


完成几个必要的方法 。就基本完成了。





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值