绘制 UITableViewCell

#import <UIKit/UIKit.h>

// to use: subclass ABTableViewCell and implement -drawContentView:

@interface ABTableViewCell : UITableViewCell
{
UIView *contentView;
}

- (void)drawContentView:(CGRect)r;// subclasses should implement

@end


#import "ABTableViewCell.h"

@interface ABTableViewCellView : UIView
@end

@implementation ABTableViewCellView

- (void)drawRect:(CGRect)r
{
[(ABTableViewCell *)[selfsuperview] drawContentView:r];
}

@end



@implementation ABTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier])) {
contentView = [[ABTableViewCellViewalloc]initWithFrame:CGRectZero];
contentView.opaque =YES;
[selfaddSubview:contentView];
}
return self;
}


- (void)setFrame:(CGRect)f
{
[supersetFrame:f];
CGRect b = [selfbounds];
b.size.height -=1; // leave room for the seperator line
[contentView setFrame:b];
}

- (void)setNeedsDisplay
{
[supersetNeedsDisplay];
[contentViewsetNeedsDisplay];
}

- (void)drawContentView:(CGRect)r
{
// subclasses should implement this
}

@end

#import <UIKit/UIKit.h>
#import "ABTableViewCell.h"

@interface CustomDrawnCell : ABTableViewCell  {
    
    NSString *_title;
    NSString *_subTitle;
    NSString *_timeTitle;
    UIImage *_thumbnail;
}

- (void)setTitle:(NSString*) title subTitle:(NSString*) subTitle time:(NSString*) time thumbnail:(UIImage *)aThumbnail;

@end

#import "CustomDrawnCell.h"

@implementation CustomDrawnCell

static UIFont *titleFont = nil;
static UIFont *subTitleFont = nil;
static UIFont *timeTitleFont = nil;

- (void)setTitle:(NSString*) aTitle subTitle:(NSString*) aSubTitle time:(NSString*) aTimeTitle thumbnail:(UIImage *)aThumbnail
{
    if (_title != aTitle) {
        _title = aTitle;        
    }
    
    if (_subTitle != aSubTitle) {
        _subTitle = aSubTitle;
    }

    if (_timeTitle != aTimeTitle) {
        _timeTitle = aTimeTitle;
    }

    if (_thumbnail != aThumbnail) {
        _thumbnail = aThumbnail;        
    }
    
    [self setNeedsDisplay];
}

+(void) initialize
{
    titleFont = [UIFont systemFontOfSize:17];
    subTitleFont = [UIFont systemFontOfSize:13];
    timeTitleFont = [UIFont systemFontOfSize:10];
}

+(void) dealloc
{
    [super dealloc];
}

-(void) drawContentView:(CGRect)r
{    
    static UIColor *titleColor;    
    titleColor = [UIColor darkTextColor];
    static UIColor *subTitleColor;    
    subTitleColor = [UIColor darkGrayColor];
    static UIColor *timeTitleColor;    
    timeTitleColor = [UIColor colorWithRed:0 green:0 blue:255 alpha:0.7];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if(self.highlighted || self.selected)
	{
		CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
		CGContextFillRect(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
		CGContextSetFillColorWithColor(context, titleColor.CGColor);					
	}
	else
	{
		CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
		CGContextFillRect(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
		CGContextSetFillColorWithColor(context, titleColor.CGColor);					
	}
    
    [titleColor set];
    [_thumbnail drawInRect:CGRectMake(12, 4, 35, 35)];
    [_title drawAtPoint:CGPointMake(54, 3) 
                    forWidth:200 
                    withFont:titleFont 
                    fontSize:17 
               lineBreakMode:UILineBreakModeTailTruncation 
          baselineAdjustment:UIBaselineAdjustmentAlignCenters];    

    [subTitleColor set];
    [_subTitle drawAtPoint:CGPointMake(54, 23) 
               forWidth:200 
               withFont:subTitleFont 
               fontSize:13 
          lineBreakMode:UILineBreakModeTailTruncation 
     baselineAdjustment:UIBaselineAdjustmentAlignCenters];    

    [timeTitleColor set];
    [_timeTitle drawAtPoint:CGPointMake(262, 3) 
                  forWidth:62 
                  withFont:timeTitleFont 
                  fontSize:10 
             lineBreakMode:UILineBreakModeTailTruncation 
        baselineAdjustment:UIBaselineAdjustmentAlignCenters];    

}

@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值