2010-09-27 47 views
0

好的,在解釋問題之前,我的應用程序有一小段簡短的內容。 我的應用程序有兩個視圖,它是自定義的TableViewCell,一個frontview和一個backview(一旦你在手機上劃過手指就會顯示,就像twitter-app一樣)。單元格子視圖上的UIButton存在問題

Anywho,我想在backview上有一些按鈕。我在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函數繪製它們。

[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子類。它是這裏的UILabels繪製

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


} 
+0

很多代碼。 phew :) – vodkhang 2010-09-27 16:50:44

+0

RadioTableCellView的子類是否有UITableViewCell?如果是這樣的話,我認爲我有一個更好的方法。 – 2010-09-27 16:54:21

+0

不,它的子類UIView,RadioTableCell另一方面,子類UITableViewCell。 – Jensen2k 2010-09-27 17:04:13

回答

2

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

+0

謝謝! :-)解決了一切! :d – Jensen2k 2010-09-27 20:24:46

2

在第一次看,我可以肯定的是,如果你繪製文本(和文本是不是標籤),那麼就沒有的UILabel都在你的細胞。你可以忽略這種方式。

所以,如果你沒有UILabel,你如何獲得文本。因爲你的contentView的確是一個RadioTableCellView,所以它並不難。在你的班級中,只需公開一個名爲nextArtist的房產。當你點擊你的按鈕時,查找contentView(通過調用一些超級視圖),將它投射到RadioTableCellView然後得到nextArtist

+0

這就是我的想法。我試着用addSubview替換drawTextInRect,但沒有效果。 – Jensen2k 2010-09-27 16:53:38

+0

你應該用addSubview發佈代碼,可能有錯誤 – vodkhang 2010-09-27 16:55:21

+0

更新了代碼。看看nowTitle,我試着在那裏添加一個子視圖。 – Jensen2k 2010-09-27 17:10:52

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子類的內容查看,不給的UITableViewCell。有關詳細信息,請參見Programmatically Adding Subviews to a Cell’s Content View

+0

說了這麼多,如果你只是想要的文本,而不是實際的控制,vodkhang的方法是要走的路。 – 2010-09-27 18:15:28