美文网首页
6.3 自定义Cell

6.3 自定义Cell

作者: 草根小强 | 来源:发表于2019-04-17 11:36 被阅读0次

    自定义Cell

    #import "ViewController.h"
    #import "MCJTableViewCell.h"
    
    @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self addTableViewToView];
    }
    
    - (void)addTableViewToView
    {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        [self.view addSubview:tableView];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 30;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        NSString *rID = @"ID";
        MCJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rID];
        if (cell == nil) {
            cell = [[MCJTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rID];
        }
        
        // 设置属性
        cell.pic.image = [UIImage imageNamed:@"2222"];
        cell.titL.text = @"我是标题";
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 100;
    }
    @end
    
    #import <UIKit/UIKit.h>
    
    @interface MCJTableViewCell : UITableViewCell
    @property (nonatomic, strong)UIImageView *pic;
    @property (nonatomic, strong)UILabel *titL;
    @end
    
    #import "MCJTableViewCell.h"
    
    @implementation MCJTableViewCell
    
    - (void)awakeFromNib {
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            
            // 添加一张图片
            _pic = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 40, 40)];
            [self.contentView addSubview:_pic];
            
            // 添加一个Lable
            _titL = [[UILabel alloc] initWithFrame:CGRectMake(130, 0, 150, 40)];
            [self.contentView addSubview:_titL];
            
        }
        return self;
    }
    @end
    
    自定义Cell.png

    相关文章

      网友评论

          本文标题:6.3 自定义Cell

          本文链接:https://www.haomeiwen.com/subject/kgqiwqtx.html