ios开发 自定义UITableView cell

UITableCellViewController.h

#import <UIKit/UIKit.h>

@interface UITableCellViewController : UITableViewCell

    @property(nonatomic,retain) UILabel *lb_title;
    @property(nonatomic,retain) UILabel *lb_date;
    @property (nonatomic,retain) UILabel *lb_points;
    
    @property(nonatomic,retain) UILabel *lb_title_content;
    @property(nonatomic,retain) UILabel *lb_date_content;
    @property (nonatomic,retain) UILabel *lb_points_content;


@end

UITableCellViewController.m

//
//  UITableCellViewController.m
//  TableViewCell001
//
//  Created by Bo Xiu on 12-9-4.
//  Copyright (c) 2012年 Bo Xiu. All rights reserved.
//

#import "UITableCellViewController.h"

@interface UITableCellViewController ()

@end

@implementation UITableCellViewController

@synthesize lb_title = _lb_title ;
@synthesize lb_date  = _lb_date ;
@synthesize lb_points = _lb_points ;

@synthesize lb_title_content = _lb_title_content ;
@synthesize lb_date_content  = _lb_date_content ;
@synthesize lb_points_content = _lb_points_content ;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id) init{
    [super init];
    
    if(self){
        [self setFrame:CGRectMake(0, 0, 320, 120)];
        self.backgroundColor = [UIColor blueColor];
        
        int height = 25 ;
        int middle_height = 5 ;
        
        UIColor * bgcolor = [UIColor yellowColor];
        
        _lb_title = [[UILabel alloc] initWithFrame:CGRectMake(20, 5 + height * 0 + middle_height * 0, 45, height )];
        _lb_title.backgroundColor = bgcolor;
        _lb_title.text = @"标题:";
        
        _lb_date = [[UILabel alloc] initWithFrame:CGRectMake(20, 5 + height * 1 + middle_height * 1, 45, height )];
        _lb_date.backgroundColor = bgcolor;
        _lb_date.text = @"时间:";
        
        _lb_points = [[UILabel alloc] initWithFrame:CGRectMake(20, 5 + height * 2+ middle_height * 2, 45, height)];
        _lb_points.backgroundColor = bgcolor;
        _lb_points.text = @"路标:";
        
        _lb_title_content = [[UILabel alloc] initWithFrame:CGRectMake(70, 5 + height * 0 + middle_height * 0, 240, height)];
        _lb_title_content.backgroundColor = bgcolor;
        _lb_title_content.text = @"--------------";
        
        _lb_date_content = [[UILabel alloc] initWithFrame:CGRectMake(70, 5 + height * 1 + middle_height * 1, 240, height)];
        _lb_date_content.backgroundColor = bgcolor;
        _lb_date_content.text = @"--------------";
        
        _lb_points_content = [[UILabel alloc] initWithFrame:CGRectMake(70, 5 + height * 2 + middle_height * 2, 240, height )];
        _lb_points_content.backgroundColor = bgcolor;
        _lb_points_content.text = @"--------------";
        
        
        
        [self addSubview:_lb_title];
        [self addSubview:_lb_points];
        [self addSubview:_lb_date];
        
        [self addSubview:_lb_title_content];
        [self addSubview:_lb_date_content];
        [self addSubview:_lb_points_content];
    }
    return  self ;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
@end

ViewController.m

//
//  ViewController.m
//  TableViewCell001
//
//  Created by Bo Xiu on 12-9-4.
//  Copyright (c) 2012年 Bo Xiu. All rights reserved.
//

#import "ViewController.h"
#import "UITableCellViewController.h"
@interface ViewController ()

@end

@implementation ViewController

@synthesize myView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [myView release];
    myView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

// 设置单元格的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}
// 设置单元个样式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
//    
//    UITableCellViewController *cell = [tableView dequeueReusableCellWithIdentifier:
//                             SimpleTableIdentifier];
//    if (cell == nil) {
//        cell = [[[UITableCellViewController alloc] initWithStyle:UITableViewCellStyleDefault
//                                       reuseIdentifier: SimpleTableIdentifier] autorelease];
//    }
//    //cell.imageView.image=image;//未选cell时的图片
//    //cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
//    cell.textLabel.text=@"hello";
//    return cell;
    static NSString *CellIdentifier = @"Cell";
    
    UITableCellViewController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell = [[[UITableCellViewController alloc ] init] autorelease] ;
    }
    
    // Configure the cell.
    //cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail");
    return cell;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (void)dealloc {
    [myView release];
    [super dealloc];
}
@end

相关推荐