2010-09-27 48 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