2011-11-07 71 views
0

我在通常的“添加自己的标签/图像到cell.contentview”从控制器的cellForRowAtIndexPath内。我试图让其中一个标签具有动画滚动文本:也就是说,如果标签中的文本溢出了viewCell宽度,则单元格中的标签应滚动文本(动画 - 不是手动)。此外,这整个tableview +滚动文本应适用于iPhone和iPad(所以一切都需要相应地自动调整大小)。UIView帧不能更新大小在UILabel中创建滚动文本动画

切正题,问题是:

如何获得cell.contentView的一个子视图来更新它的框架尺寸上的时间进行测试,如果该子视图溢出中的文本或没有。

或者,这是甚至正确的方式去呢?

这里的(等说明如下secondLabel)我的意思的图像:

现在,您可以继续阅读细节:

我试着去了解事情是这样的通过cellForRowAtIndexPath将自定义的UISCrollLabelView实例添加到单元格的contentView中。这个自定义类实际上是一个UIView,它在内部管理一个UILabel。

UISCrollLabelView应自动调整大小不会溢出表格单元格(应该适用于iPhone和iPad)。然后,根据cellForRowAtIndexPath传递给它的文本的长度,它应该自动调整内部标签的大小以适应所有文本,并且如果标签结束溢出视图(self),则为UILabel的滚动设置动画。

我对此有几个问题,但主要是:UIScrollableView基于比较其内部标签的frame.size.width与self.frame.size.width来触发(或不)动画。但显然,自身的帧宽需要一些时间才能从0更新到它在插入cell.contenView之后最终调整大小的大小。这意味着,当我的自定义UIScrollLabelView测试(label.frame.size.width > self.frame.size.width)这总是如此,并且不溢出的文本仍然滚动。第二秒左右,self.frame会更新为正确的大小。

这里的cellForRowAtIndexPath(secondLabel是这里的滚动视图):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIImageSmartView *cachedImage; 
    UILabel *mainLabel; 
    UIScrollLabelView *secondLabel; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier] autorelease]; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 

    mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(70, 0, 0, 20)] autorelease]; 
    mainLabel.font = [UIFont boldSystemFontOfSize:18]; 
    mainLabel.textAlignment = UITextAlignmentLeft; 
    mainLabel.textColor = [UIColor blackColor]; 
    mainLabel.backgroundColor = [UIColor clearColor]; 
    mainLabel.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin); 
    mainLabel.tag = MAINLABEL_TAG; 

    secondLabel = [[[UIScrollLabelView alloc] initWithFrame:CGRectMake(70, 40, 0, 20)] autorelease]; 
    secondLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin); 
    //secondLabel.backgroundColor = [UIColor clearColor]; 
    secondLabel.tag = SECONDLABEL_TAG; 

    cachedImage = [[UIImageSmartView alloc] initWithFrame:CGRectMake(5, 5, 57, 80)]; 
    cachedImage.tag = PHOTO_TAG; 

    [cell.contentView addSubview:cachedImage]; 
    [cell.contentView addSubview:mainLabel]; 
    [cell.contentView addSubview:secondLabel]; 

    } 
    else 
    { 
    cachedImage = (UIImageSmartView*)[cell.contentView viewWithTag:PHOTO_TAG]; 
    mainLabel = (UILabel*)[cell.contentView viewWithTag:MAINLABEL_TAG]; 
    secondLabel = (UIScrollLabelView*)[cell.contentView viewWithTag:SECONDLABEL_TAG]; 
    } 

    // Configure the cell... 

    NSString *ImageName = [[dbData objectAtIndex:indexPath.row] objectAtIndex:2]; 
    NSString *imageURL = [NSString stringWithFormat:@"http://someserver", referencingTable]; 
    [cachedImage loadAndCacheImageFromFile:ImageName fromURL:imageURL inSize:CGSizeMake(57, 80) withBorderWidth:4]; 

    mainLabel.text = [[dbData objectAtIndex:indexPath.row] objectAtIndex:0]; 

    // -> At this point I load the text into the "scrolling label" 
    // (actually a UIView with a label) 
    [secondLabel setScrollingText:[[dbData objectAtIndex:indexPath.row] objectAtIndex:1]]; 

    return cell; 
} 

而且我UIScrollLabelView实现看起来像这样至今:

@implementation UIScrollLabelView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     // Initialization code 
     isScrolling = NO; 

     self.clipsToBounds = YES; 
     UILabel *label = [[[UILabel alloc] init] autorelease]; 
     label.font = [UIFont systemFontOfSize:14.0]; 
     label.textAlignment = UITextAlignmentLeft; 
     label.textColor = [UIColor darkGrayColor]; 
     label.backgroundColor = [UIColor greenColor]; 
     self.backgroundColor = [UIColor redColor]; 
     //label.backgroundColor = [UIColor clearColor]; 

     [self addSubview: label]; 

    } 
    return self; 
} 

- (void)setScrollingText:(NSString*)text 
{ 
    [self setNeedsLayout]; 
    UILabel *label = [[self subviews ] objectAtIndex:0]; 
    label.text = text; 
    [label sizeToFit]; 

    if(label.frame.size.width > self.frame.size.width) 
    [self scrollText]; 

} 

- (void)scrollText 
{ 
    if(isScrolling) 
     return; 

    isScrolling = YES; 

    UILabel *label = [[self subviews ] objectAtIndex:0]; 

    [UIView beginAnimations:@"scroll" context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationDidStopSelector:@selector(scrollDidComplete)]; 
    [UIView setAnimationDuration: label.frame.size.width/(float)100]; 

    CGRect frame = label.frame; 
    if(frame.origin.x == 0) 
     frame.origin.x = frame.origin.x - (frame.size.width - self.frame.size.width); 
    else 
     frame.origin.x = 0; 

    label.frame = frame;  
    [UIView commitAnimations]; 

} 

- (void)scrollDidComplete 
{ 
    isScrolling = NO; 
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scrollText) userInfo:nil repeats:NO]; 
} 


@end 

回答

0

您设置标签的尺寸时,单元格的大小尚未定义。然后你再也不检查实际大小。 您需要重写setFrame和setBounds以捕获帧大小更改。