开发者

Problem with UIButton on subview of a cell

Okay, a short breif of my app before i explain the problem. My app has two views for it's custom TableViewCell, one frontview, and one backview(which is revealed once you swipe your finger across the cell, much like the twitter-app).

Anywho, i wanted to have some buttons on the backview. I did this in the cellForRowAtIndexPath-method

The first you'll see here is how i assign labels to the cells. The secondary thing you'll see is the button. It's working fine.

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

But, it's in the next method the problem occours.

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

    }
}

So, i realised that i cannot just go to the superview of the button and find my labels there, because their in another subview. I have scanned all my views, but still cannot find the label.

I am very new at this, and the subclassing of the TableView-cell is something i implemented from someone who posted their code.

But, my assumption is that there are noe UILabels in my view, because i am not adding them as views, only drawing them, with the drawTextInRect-function.

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

I have tried to adding them as subviews, but with no luck. Could anyone help me?

// Some more code you may need to solve the puzzle(this is where the cell-views are made)

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

Please help me, or at least point me in a direction or two!

////////////////////////// Updated with some new code: This is inside my RadioCustomCell, a UIView-subclass. It's here the UILabels are drawn

#import "RadioCustomCell.h"

@implementation RadioCustomCe开发者_开发问答ll
@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)];


}


You just forgot to init your UILabel in your very first line of code. :)


At the first look, I can be sure that if you draw the text (and the text is not the label), then there is no UILabel at all in your cell. You can ignore that way.

So, if you don't have the UILabel, how you can get the text. Because your contentView is indeed a RadioTableCellView, then it is not hard. In your class, just public a property called nextArtist. When your button is tapped, look up for the contentView (by calling some superview), cast it to RadioTableCellView then get the nextArtist out


Without writing a bunch of code, here's my best guess.

You're going to need to assign tags to aView and anotherView in your initWithStyle: method. I'm going to assume you have a couple constants: BACK_VIEW_TAG and FRONT_VIEW_TAG.

In your touched: method, walk up the view hierarchy until you find your UITableViewCell.

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

Get the front view and back view (or which ever you want) from the table cell's contentView using the tags.

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:.
}

Note that you should be adding views to your UITableViewCell subclass's contentView, not to the UITableViewCell directly. See Programmatically Adding Subviews to a Cell’s Content View for details.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜