首页 > 其他 > 详细

自定义TableViewCell

时间:2014-10-13 22:53:18      阅读:441      评论:0      收藏:0      [点我收藏+]

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;
}

 

自定义TableViewCell

原文:http://www.cnblogs.com/code-style/p/4023204.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!