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;
    }
  • 相关阅读:
    c#基于业务对象的筛选
    SQLServer索引调优实践
    C#中抽象类和接口的区别
    c#基础(2) 理解委托和事件
    建议学习jQuery的步骤!
    SQL SERVER存储过程调用存储过程并接收输出参数或返回值的方法
    ASP.NET基于JQUERY的高性能的TreeView
    GetManifestResourceStream得到的Stream是null的解决
    Using GDI+ on Windows Mobile 初体验
    提供一个Windows mobile Native UI 程序,循序渐进开发,并附有代码!
  • 原文地址:https://www.cnblogs.com/code-style/p/4023204.html
Copyright © 2011-2022 走看看