UIButton在单元子视图上的问题

时间:2010-09-27 16:44:11

标签: iphone objective-c uitableview uibutton subview

好的,在我解释问题之前,我的应用程序的简短版本。 我的应用程序有两个视图,它的自定义TableViewCell,一个前视图和一个后视图(一旦你在单元格上滑动手指就会显示,就像twitter-app一样)。

Anywho,我想在backview上有一些按钮。我是在cellForRowAtIndexPath-method

中完成的

你在这里看到的第一个是我如何为细胞分配标签。您将看到的第二件事是按钮。它工作正常。

UILabel *nextArtist = [UILabel alloc];
        nextArtist.text = @"Rihanna";
        nextArtist.tag = 4;
        [cell setNextArtist:nextArtist];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(6 ,31, 110, 20);
        [button setImage:[UIImage imageNamed:@"radionorge.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];



        [cell.backView addSubview:button];

但是,问题出现在下一个方法中。

-(void)touched:(id)sender {

    // Here i want to get the UILabels for each cell. Such as nextArtist.
    if ([sender isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)sender;
        UIView *contentView = button.superview;
        UIView *viewWithTag4 = [contentView viewWithTag:4];
        if ([viewWithTag1 isKindOfClass:[UILabel class]]) {
            UILabel *titleLabel = (UILabel *)viewWithTag4;
            NSLog(@"Label: ",titleLabel.text);
        }

    }
}

所以,我意识到我不能只是去按钮的超级视图并在那里找到我的标签,因为他们在另一个子视图中。我扫描了所有的观点,但仍然找不到标签。

我对此非常陌生,而TableView-cell的子类化是我发布代码的人实现的。

但是,我的假设是我的视图中没有UILabel,因为我没有将它们添加为视图,只使用drawTextInRect函数绘制它们。

[nextArtist drawTextInRect:CGRectMake(boundsX+200 ,46, 110, 15)];

我试图将它们添加为子视图,但没有运气。有谁可以帮助我?

//您可能需要更多代码来解决这个难题(这是制作单元格视图的地方)

@implementation RadioTableCellView
- (void)drawRect:(CGRect)rect {

    if (!self.hidden){
        [(RadioTableCell *)[self superview] drawContentView:rect];
    }
    else
    {
        [super drawRect:rect];
    }
}
@end

@implementation RadioTableCellBackView
- (void)drawRect:(CGRect)rect {

    if (!self.hidden){
        [(RadioTableCell *)[self superview] drawBackView:rect];
    }
    else
    {
        [super drawRect:rect];
    }
}

@end

@interface RadioTableCell (Private)
- (CAAnimationGroup *)bounceAnimationWithHideDuration:(CGFloat)hideDuration initialXOrigin:(CGFloat)originalX;
@end

@implementation RadioTableCell
@synthesize contentView;
@synthesize backView;
@synthesize contentViewMoving;
@synthesize selected;
@synthesize shouldSupportSwiping;
@synthesize shouldBounce;

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

    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {

        [self setBackgroundColor:[UIColor clearColor]];

        RadioTableCellView * aView = [[RadioTableCellView alloc] initWithFrame:CGRectZero];
        [aView setClipsToBounds:YES];
        [aView setOpaque:YES];
        [aView setBackgroundColor:[UIColor clearColor]];
        [self setContentView:aView];
        [aView release];

        RadioTableCellBackView * anotherView = [[RadioTableCellBackView alloc] initWithFrame:CGRectZero];
        [anotherView setOpaque:YES];
        [anotherView setClipsToBounds:YES];
        [anotherView setHidden:YES];
        [anotherView setBackgroundColor:[UIColor clearColor]];
        [self setBackView:anotherView];
        [anotherView release];

        // Backview must be added first!
        // DO NOT USE sendSubviewToBack:

        [self addSubview:backView];
        [self addSubview:contentView];

        [self setContentViewMoving:NO];
        [self setSelected:NO];
        [self setShouldSupportSwiping:YES];
        [self setShouldBounce:YES];
        [self hideBackView];
    }

    return self;
}

请帮帮我,或者至少指点一两个方向!

////////////////////////// 更新了一些新代码: 这是我的RadioCustomCell里面的一个UIView子类。在这里绘制了UILabel

#import "RadioCustomCell.h"

@implementation RadioCustomCell
@synthesize nowTitle,nowArtist,nextTitle,nextArtist,ChannelImage;

// Setting the variables

- (void)setNowTitle:(UILabel *)aLabel {

    if (aLabel != nowTitle){
        [nowTitle release];
        nowTitle = [aLabel retain];
        [self setNeedsDisplay];
    }
}

- (void)setNowArtist:(UILabel *)aLabel {

    if (aLabel != nowArtist){
        [nowArtist release];
        nowArtist = [aLabel retain];
        [self setNeedsDisplay];
    }
}
- (void)setNextTitle:(UILabel *)aLabel {

    if (aLabel != nextTitle){
        [nextTitle release];
        nextTitle = [aLabel retain];
        [self setNeedsDisplay];
    }
}
- (void)setNextArtist:(UILabel *)aLabel {

    if (aLabel != nextArtist){
        [nextArtist release];
        nextArtist = [aLabel retain];
        [self setNeedsDisplay];
    }
}

- (void)setChannelImage:(UIImage *)aImage {

    if (aImage != ChannelImage){
        [ChannelImage release];
        ChannelImage = [aImage retain];
        [self setNeedsDisplay];
    }
}

- (void)drawContentView:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    //UIColor * backgroundColour = [UIColor whiteColor];

    UIColor *backgroundColour = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"CellBackground.png"]];

    [backgroundColour set];
    CGContextFillRect(context, rect);

    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;

    [ChannelImage drawInRect:CGRectMake(boundsX+120 ,25, 75, 35)];

    nowTitle.enabled = YES;
    nowTitle.textAlignment = UITextAlignmentCenter;
    nowTitle.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size: 14.0];
    nowTitle.textColor = [UIColor blackColor];
    nowTitle.backgroundColor = [UIColor clearColor];
    //[nowTitle drawTextInRect:CGRectMake(boundsX+6 ,31, 110, 20)];

    // Trying to add a subview instead of drawing the text
    nowTitle.frame = CGRectMake(boundsX+6 ,31, 110, 20);
    [self addSubview:nowTitle];
    // I have also tried adding it to super, no effect.

    nowArtist.enabled = YES;
    nowArtist.textAlignment = UITextAlignmentCenter;
    nowArtist.font = [UIFont fontWithName:@"HelveticaNeue" size: 10.0];
    nowArtist.textColor = [UIColor blackColor];
    nowArtist.backgroundColor = [UIColor clearColor];
    [nowArtist drawTextInRect:CGRectMake(boundsX+6 ,46, 110, 15)];

    nextTitle.enabled = NO;
    nextTitle.textAlignment = UITextAlignmentCenter;
    nextTitle.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size: 12.0];
    [nextTitle drawTextInRect:CGRectMake(boundsX+200 ,31, 110, 20)];

    nextArtist.enabled = NO;
    nextArtist.textAlignment = UITextAlignmentCenter;
    nextArtist.font = [UIFont fontWithName:@"HelveticaNeue" size: 9.0];
    [nextArtist drawTextInRect:CGRectMake(boundsX+200 ,46, 110, 15)];


}

3 个答案:

答案 0 :(得分:2)

初看起来,我可以肯定,如果您绘制文本(并且文本不是标签),那么您的单元格中根本没有UILabel。你可以忽略这一点。

因此,如果您没有UILabel,那么如何获取文本。因为你的contentView确实是RadioTableCellView,所以并不难。在您的课程中,只需公开一个名为nextArtist的属性。点按您的按钮后,请查看contentView(通过调用某个超级视图),将其投放到RadioTableCellView然后获取nextArtist

答案 1 :(得分:2)

您只是忘了在第一行代码中初始化您的UILabel。 :)

答案 2 :(得分:0)

不写一堆代码,这是我最好的猜测。

您需要在initWithStyle:方法中为aView和anotherView分配标签。我假设您有几个常量:BACK_VIEW_TAGFRONT_VIEW_TAG

touched:方法中,向上走视图层次结构,直到找到UITableViewCell。

UIView *currentView = button.superView;
while (![currentView isKindOfClass:UITableViewCell] && currentView != nil) {
    currentView = currentView.parent;
}

使用标签从表格单元格的contentView获取前视图和后视图(或任何您想要的视图)。

if (currentView != nil) {
    UITableViewCell *cellView = (UITableViewCell *)currentView)
    UIView *cellContentView = cellView.contentView;
    UIView *backView = [cellContentView viewWithTag:BACK_VIEW_TAG];
    UIView *frontView = [cellContentView viewWithTag:FRONT_VIEW_TAG];

    // Get other views from frontView and backView using viewWithTag:.
}

请注意,您应该将视图添加到UITableViewCell子类的contentView,而不是直接添加到UITableViewCell。有关详细信息,请参阅Programmatically Adding Subviews to a Cell’s Content View