开发者

ABTableViewCell - Adding a UIButton

I have been using the ABTableViewCell to crea开发者_JS百科te fast scrolling cells. All is working great apart from I can't figure out how to add a UIButton to my custom cell.

When using ABTableViewCell you do all the drawing of the text/images yourself using drawAtPoint or drawInRect. However UIButton doesn't support any of these methods. You can't do addSubView as the whole point of using ABTableViewCell is that you only have one view.

Anyone had any idea of how to do this?

Thanks


I have a "share" button inside a ABTableViewCell sublcass.

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

I use a yellow rect to debut the active area of the button. In drawRect callback:

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];
    }

Handle the touch:

-(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];
}


One alternative to Loren Brichter's ABTableViewCell is setting the -shouldRasterize: flag on UITableViewCell's backing CALayer to YES. -shouldRasterize: will draw your cell off-screen and cache it. This is a good approach if your cells are generally pretty simple and homogenous. To access these layer properties, remember that you'll have to link against the QuartzCore framework.

Keep in mind that this approach will generally not give you the same results if you are animating in your cell as -shouldRasterize: will attempt to draw and cache for every frame of the animation.


Make a subclass of it should work. Do the following :

When defining the cell, do this :

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

MyFunkyCellClass.h:

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

in 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;
}

This is roughly how it should be done :]


UITableViewCell is a subclass of UIResponder, so you could always consider handling touches manually in a specific area of the cell's bounds without the use a subview (flat views being one of the key ABTableViewCell approaches for fast scrolling).

Then, you could set up your own delegation or target/action mechanism to have your cell inform other areas of the application about interaction, or just draw the button area of the cell differently.

Also, if you override UIResponder methods and do not handle the input, be sure to call super so the UITableView will recognized row selection, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜