簡體   English   中英

單元格子視圖上的UIButton問題

[英]Problem with UIButton on subview of a cell

好的,在我解釋問題之前,請先簡短介紹我的應用。 我的應用程序有兩個視圖,分別是自定義TableViewCell,一個正視圖和一個后視圖(當您在單元格上滑動手指時就會顯示出來,就像twitter-app一樣)。

任何人,我想在后視圖上有一些按鈕。 我是在cellForRowAtIndexPath方法中完成的

您將在這里首先看到的是我如何為單元分配標簽。 您將看到的第二件事是按鈕。 一切正常。

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的子類是我從發布其代碼的人那里實現的。

但是,我的假設是我的視圖中沒有UILabels,因為我沒有將它們添加為視圖,而只是使用drawTextInRect-function繪制它們。

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


}

乍一看,我可以確定如果繪制文本(文本不是標簽),則單元格中根本沒有UILabel。 您可以忽略這種方式。

因此,如果您沒有UILabel ,那么如何獲取文本。 因為您的contentView實際上是RadioTableCellView ,所以並不難。 在您的課堂上,只需公開一個名為nextArtist的屬性。 輕按按鈕時,查找contentView (通過調用一些RadioTableCellView視圖),將其投射到RadioTableCellView然后取出nextArtist

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

無需編寫大量代碼,這是我的最佳猜測。

您將需要在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中。 有關詳細信息,請參見以編程方式將子視圖添加到單元的內容視圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM