绘制 UITableViewCell

来源:互联网 发布:广电网络 股票 编辑:程序博客网 时间:2024/06/06 01:14

#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 CustomDrawnCellstatic 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
原创粉丝点击