2011-02-01 49 views
2

我一直在使用ABTableViewCell創建快速滾動單元格。除了我不知道如何將UIButton添加到我的自定義單元格,所有工作都很好。ABTableViewCell - 添加UIButton

使用ABTableViewCell時,使用drawAtPoint或drawInRect自己完成文本/圖像的所有繪製。但是UIButton不支持任何這些方法。您不能使用addSubView作爲使用ABTableViewCell的全部要點,因爲您只有一個視圖。

任何人有任何想法如何做到這一點?

謝謝

回答

5

我在ABTableViewCell sublcass裏有一個「share」按鈕。

static UIImage* imgShare=nil; 
    + (void)initialize 
    { 
     if(self == [MyCustomCell class]) 
     { 
      imgShare = [[UIImage imageNamed:@"share.png"] retain];  
     } 
    } 

我用一個黃色矩形來登場按鈕的活動區域。 在回調的drawRect:

CGRect btnShareActiveAreaRect; 
    - (void)drawContentView:(CGRect)r 
    { 
     [[UIColor yellowColor] set]; 
     btnShareActiveAreaRect = CGRectMake(10,10,65,45); 
     CGContextFillRect(context, btnShareActiveAreaRect); 
     CGRect shareRect = CGRectMake(15,20,25,19); 
     [imgShare drawInRect:shareRect]; 
    } 

操作觸摸:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch =[touches anyObject]; 
    CGPoint startPoint =[touch locationInView:self.contentView]; 
    if(CGRectContainsPoint(btnShareActiveAreaRect,startPoint)) 
    { 
     [self shareButton_Click]; 
    } 
    else 
     [super touchesBegan:touches withEvent:event]; 
} 
0

使它的一個子類應該工作。請執行以下操作:

當定義單元格,這樣做:

MyFunkyCellClass *cell = (MyFunkyCellClass*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

MyFunkyCellClass.h:

#import <UIKit/UIKit.h> 
@interface MyFunkyCellClass : UITableViewCell { 
    UIButton *fancyButton; 
} 
@property (nonatomic, retain) UIButton *fancyButton; 
@end 

在MyFunkyCellClass.h:

@synthesize fancyButton; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     fancyButton = [[UIButton alloc] initWithFrame:CGRectMake(113, 2, 113, 31)]; 
[self addSubview:fancyButton]; 
} 

    return self; 
} 

這大致如何它應該做的事:]

0

UITableViewCellUIResponder子類,所以你總是可以考慮在小區邊界的一個特定區域手動操作觸摸,而無需使用一個子視圖(平面視圖是快速的關鍵ABTableViewCell途徑之一滾動)。

然後,您可以設置您自己的委託或目標/操作機制,讓您的單元通知應用程序的其他區域有關交互,或者僅以不同方式繪製單元格的按鈕區域。

此外,如果重寫UIResponder方法和不處理的投入,一定要打電話super所以UITableView會認可行選擇等

1

一種可以替代的羅蘭Brichter的ABTableViewCell被設置-shouldRasterize:標誌上的UITableViewCell的支持CALayer到YES-shouldRasterize:會將您的手機畫面關閉並緩存。如果你的細胞一般非常簡單和同質,這是一個很好的方法。要訪問這些圖層屬性,請記住,您必須鏈接到QuartzCore框架。

請記住,如果您在單元格中進行動畫製作,此方法通常不會給您相同的結果,因爲-shouldRasterize:將嘗試爲動畫的每個幀繪製和緩存。

+0

謝謝,我不知道該設置,我會試試看。比檢測自己觸摸要容易得多,因爲我是我正在走下的路線。雖然如果是柵格化圖層,它是否仍然響應按鈕事件? – Toby 2011-02-03 08:49:22

+0

它的工作原理完全相同,除了手動呈現該單元格的最終合成視圖。我還應該指出,它僅適用於iOS 3.2+。 – 2011-02-03 18:35:23