我用它在我的應用程序之一,我有一個細胞 的多重表達我有一個有邊界的單元格和一個有額外的單元格按鈕和有紋理的圖象單元 我還需要一鍵
這裏的點擊來改變他們的是一些我用
//CustomCell.h
@interface CustomCell : UIView
//CustomCell.m
@implementation CustomCell
- (void)drawRect:(CGRect)rect
{
//Draw the normal images on the cell
}
@end
而對於自定義單元格邊境的代碼
//CellWithBorder.h
@interface CellWithBorder : CustomCell
{
CustomCell *aCell;
}
//CellWithBorder.m
@implementation CellWithBorder
- (void)drawRect:(CGRect)rect
{
//Draw the border
//inset the rect to draw the original cell
CGRect insetRect = CGRectInset(rect, 10, 10);
[aCell drawRect:insetRect];
}
現在,在我的視圖控制器,我會做以下
CustomCell *cell = [[CustomCell alloc] init];
CellWithBorder *cellWithBorder = [[CellWithBorder alloc] initWithCell:cell];
如果以後我想切換到另一個細胞我會做
CellWithTexture *cellWithBorder = [[CellWithTexture alloc] initWithCell:cellWithBorder.cell];
@RobNapier不,這是裝飾,裝飾器是一個「子類」,即「有一個」參考超類,裝飾也必須符合超類接口 –
你是對的;我誤解了代碼。 –
@RobNapier :),我愛你的書btw –