zoukankan      html  css  js  c++  java
  • 自定义TableViewCell

    BookStoreCell.h

    #import <UIKit/UIKit.h>
    
    @interface BookStoreCell : UITableViewCell
    
    @property (strong, nonatomic) UIImageView *bookImageView;
    @property (strong, nonatomic) UILabel *nameLabel;
    @property (strong, nonatomic) UILabel *authorLabel;
    @property (strong, nonatomic) UILabel *summaryLabel;
    @end

    BookStoreCell.m

    #import "BookStoreCell.h"
    
    @implementation BookStoreCell
    @synthesize bookImageView;
    @synthesize nameLabel;
    @synthesize authorLabel;
    @synthesize summaryLabel;
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        NSLog(@"INIT");
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            [self initSubview];
        }
        return self;
    }
    
    - (void)initSubview
    {
        CGRect rect = self.frame;
        
        bookImageView = [[UIImageView alloc]init];
        bookImageView.frame = CGRectMake(10, 2, 55, 75);
        bookImageView.backgroundColor = [UIColor yellowColor];
        [self addSubview:bookImageView];
        
        
        nameLabel = [[UILabel alloc]init];
        nameLabel.frame = CGRectMake(75, 2, rect.size.width - 75, 50);
        nameLabel.font = [UIFont fontWithName:@"Arial" size:20];
        [self addSubview:nameLabel];
        
        
        authorLabel = [[UILabel alloc]init];
        authorLabel.frame = CGRectMake(75, 42, rect.size.width - 75, 30);
        authorLabel.font = [UIFont fontWithName:@"Arial" size:15];
        [self addSubview:authorLabel];
        
        summaryLabel = [[UILabel alloc]init];
        //[self addSubview:summaryLabel];
    }
    
    - (void)awakeFromNib
    {
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        //[super setSelected:selected animated:animated];
    }
    
    @end

    使用自定义的Cell

    #import "BookStoreViewController.h"
    #import "BookStoreCell.h"
    
    @interface BookStoreViewController ()
    
    @end
    
    @implementation BookStoreViewController
    
    - (id)initWithStyle:(UITableViewStyle)style
    {
        
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 注册自定义的TableViewCell
        [self.tableView registerClass: [BookStoreCell class] forCellReuseIdentifier:@"MYCell"];
        self.tableView.backgroundColor = [UIColor lightGrayColor];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 6;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 用注册的名字实例化Cell
        BookStoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYCell" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[BookStoreCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MYCell"];
        }
        cell.nameLabel.text = @"笑傲江湖";
        cell.authorLabel.text = @"金庸";
        return cell;
    }
    
    -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //行选中事件
        NSLog(@"%ld", (long)indexPath.row);
        
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 80;
    }
  • 相关阅读:
    对MVC模型的自悟,详尽解释,为了更多非计算机人员可以理解
    openSUSE leap 42.3 实现有线 无线同时用
    Fedora27 源配置
    Ubuntu16.04添加HP Laserjet Pro M128fn打印机和驱动
    openSUSE leap 42.3 添加HP Laserjet Pro M128fn打印机和驱动
    OpenSUSE Leap 42.3下通过Firefox Opera Chromium浏览器直接执行java应用程序(打开java jnlp文件)实现在服务器远程虚拟控制台完成远程管理的方法
    OpenSUSE Leap 42.3 安装java(Oracle jre)
    linux下支持托盘的邮件客户端Sylpheed
    Ubuntu下通过Firefox Opera Chromium浏览器直接执行java应用程序(打开java jnlp文件)实现在服务器远程虚拟控制台完成远程管理的方法
    Firefox 浏览器添加Linux jre插件
  • 原文地址:https://www.cnblogs.com/code-style/p/4023204.html
Copyright © 2011-2022 走看看