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;
    }
  • 相关阅读:
    Navicat for MySQL破解版安装
    LACP学习笔记
    MUX VLAN
    Beyond Compare用于文件比较还是蛮好的选择,特别是我们程序袁用于比较两个项目的时候,最初使用的是Beyond Compare3一直用着挺好的,几年前更新了版本4,用着用着就提示试用期30天已过期,于是我尝试如下步骤:
    思科交换机如何进行备份与还原?
    vSphere ESXi 6.7 注册码(有效)
    VMware ESXi 6.7密码正确不能登录
    Esxi 6.5 6.7的root密码经过一段时间就不可用的解决方法
    Windows Server 2012 R2 安装密钥
    ubuntu 16 添加多个IP
  • 原文地址:https://www.cnblogs.com/code-style/p/4023204.html
Copyright © 2011-2022 走看看