Iphone ABTableViewCell-添加UIButton

Iphone ABTableViewCell-添加UIButton,iphone,objective-c,uitableview,uibutton,Iphone,Objective C,Uitableview,Uibutton,我一直在使用ABTableViewCell创建快速滚动单元格。除了我不知道如何将UIButton添加到我的自定义单元格之外,一切都很好 使用ABTableViewCell时,您可以使用drawAtPoint或drawInRect自己绘制所有文本/图像。但是UIButton不支持这些方法中的任何一种。您不能添加子视图,因为使用ABTableViewCell的整个意义在于您只有一个视图 有人知道怎么做吗 谢谢让它的子类可以工作。请执行以下操作: 定义单元格时,请执行以下操作: MyFunkyCell

我一直在使用ABTableViewCell创建快速滚动单元格。除了我不知道如何将UIButton添加到我的自定义单元格之外,一切都很好

使用ABTableViewCell时,您可以使用drawAtPoint或drawInRect自己绘制所有文本/图像。但是UIButton不支持这些方法中的任何一种。您不能添加子视图,因为使用ABTableViewCell的整个意义在于您只有一个视图

有人知道怎么做吗


谢谢

让它的子类可以工作。请执行以下操作:

定义单元格时,请执行以下操作:

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

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

<>这大概是应该做的事情:[/p> <代码> UITababVIEW Cys是代码> UIResponder < /COD>的子类,因此您可以考虑在单元格边界的特定区域手动处理触摸,而不使用子视图(平面视图是键<代码> ababelVIEWCELL 快速滚动的方法之一)。 然后,您可以设置自己的委托或目标/操作机制,让您的单元格通知应用程序的其他区域有关交互,或者只是以不同方式绘制单元格的按钮区域


此外,如果您覆盖
ui响应程序
方法而不处理输入,请确保调用
super
,以便
ui表格视图
将识别行选择,等等。

替代Loren Brichter的ABTableViewCell的另一种方法是将UITableViewCell的backing CALayer上的
-shouldRasterize:
标志设置为
YES
<代码>-应光栅化:将把您的单元格从屏幕上拉出来并缓存。如果您的细胞通常非常简单且同质,那么这是一个很好的方法。要访问这些图层属性,请记住,必须链接QuartzCore框架

请记住,如果您在单元格中制作动画,这种方法通常不会给您相同的结果,因为
-应该光栅化:
将尝试为动画的每个帧绘制和缓存。

我在ABTableViewCell子容器中有一个“共享”按钮

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

谢谢,我不知道这个设置,我会试试的。比发现我自己更容易,因为我是我要走的路线。尽管它正在栅格化图层,但它仍然会响应按钮事件吗?它的工作原理完全相同,只是该单元的最终合成视图是在手动渲染之前进行的。我还应该注意,它只在iOS 3.2+上可用。我使用这种方法使它工作。不幸的是,性能远不如使用ABTableViewCell时平稳。这可能是因为我有一个背景图像和一些透明背景的UILabel。谢谢,这就是我最后所做的。不仅仅是使用UIButton,还有更多的工作要做。如果你想要自定义imgShare图像,你会怎么做?那个么你们会把图像放在哪里呢?谢谢