#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
本文详细介绍了如何实现并自定义TableViewCell,包括子类化、界面布局、内容绘制及属性设置,旨在提供一个深入理解TableViewCell自定义实现的实例。

被折叠的 条评论
为什么被折叠?



