2011-12-21 15 views
0

tableViewセクション1でUITableViewCellのtextLabelを置き換えるためにTTTAttributedLabelを使用しています。ただし、TTTAttributedLabelはスクロール時にnilを返して、ラベルのテキストをセクション1の最初のセルのテキストに変更させますUITableViewCellでUILabelがnilになる

どうすればこのようなことを防ぐことができますか教えてください。

ありがとうございました。

あなただけindexPath.section == 1セルにTTTAttributedLabelを追加しているXcodeDev

あなたのコードで
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"cellID"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease]; 
     if (indexPath.section == 1) { 
      if ([cell viewWithTag:05] == nil) { 
       TTTAttributedLabel *label = [[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(15, 5, 270, 30)] autorelease]; 
       [label setTag:05]; 
       [label setTextColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.75]]; 
       [label setBackgroundColor:[UIColor clearColor]]; 
       [cell addSubview:label]; 
      } 
     } 
    } 

    if (indexPath.section == 0) { 
     NSArray *array = [NSArray arrayWithObjects:@"Likes", @"Posts", @"Comments", nil]; 
     [cell.textLabel setText:[array objectAtIndex:indexPath.row]]; 
     [cell setBackgroundColor:[UIColor whiteColor]]; 
     [cell.textLabel setTextColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.75]]; 
     [cell.textLabel setFont:[UIFont boldSystemFontOfSize:cell.textLabel.font.pointSize]]; 
    } 
    else if (indexPath.section == 1) { 
     TBNotification *notif = [notifications objectAtIndex:indexPath.row]; 

     if ([notif.behavior isEqualToString:@"like"]) { 

      NSMutableString *str = [NSMutableString string]; 

      [str appendString:[[notif actor] stringByAppendingFormat:@" liked your post %@", notif.postTitle]]; 

      TTTAttributedLabel *label = (TTTAttributedLabel *) [cell viewWithTag:05]; 

      [label setText:str afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) { 
       NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]); 

       NSRegularExpression *regexp = NameRegularExpression(); 
       NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange]; 
       UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16]; 
       CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); 
       if (boldFont) { 
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:nameRange]; 
        CFRelease(boldFont); 
       } 

       [mutableAttributedString replaceCharactersInRange:nameRange withString:[[mutableAttributedString string] substringWithRange:nameRange]]; 

       stringRange = [[mutableAttributedString string] rangeOfString:notif.postTitle]; 

       boldSystemFont = [UIFont boldSystemFontOfSize:16]; 
       boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); 
       if (boldFont) { 
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:stringRange]; 
        CFRelease(boldFont); 
       } 

       [mutableAttributedString replaceCharactersInRange:stringRange withString:[[mutableAttributedString string] substringWithRange:stringRange]]; 
       return mutableAttributedString; 
      }]; 
     } 
     else if ([notif.behavior isEqualToString:@"new_follow"]) { 

      NSMutableString *str = [NSMutableString string]; 

      [str appendString:[[notif actor] stringByAppendingString:@" is now following you."]]; 

      TTTAttributedLabel *label = (TTTAttributedLabel *) [cell viewWithTag:05]; 

      [label setText:str afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) { 
       NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]); 

       NSRegularExpression *regexp = NameRegularExpression(); 
       NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange]; 
       UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16]; 
       CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); 
       if (boldFont) { 
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:nameRange]; 
        CFRelease(boldFont); 
       } 

       [mutableAttributedString replaceCharactersInRange:nameRange withString:[[mutableAttributedString string] substringWithRange:nameRange]]; 

       return mutableAttributedString; 
      }]; 
     } 
     else if ([[notif behavior] isEqualToString:@"new_comment"]) { 
      NSMutableString *str = [NSMutableString string]; 

      [str appendString:[[notif actor] stringByAppendingFormat:@" commented on your post %@", notif.postTitle]]; 

      TTTAttributedLabel *label = (TTTAttributedLabel *) [cell viewWithTag:05]; 

      [label setText:str afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) { 
       NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]); 

       NSRegularExpression *regexp = NameRegularExpression(); 
       NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange]; 
       UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16]; 
       CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); 
       if (boldFont) { 
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:nameRange]; 
        CFRelease(boldFont); 
       } 

       [mutableAttributedString replaceCharactersInRange:nameRange withString:[[mutableAttributedString string] substringWithRange:nameRange]]; 

       stringRange = [[mutableAttributedString string] rangeOfString:notif.postTitle]; 

       boldSystemFont = [UIFont boldSystemFontOfSize:16]; 
       boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); 
       if (boldFont) { 
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:stringRange]; 
        CFRelease(boldFont); 
       } 

       [mutableAttributedString replaceCharactersInRange:stringRange withString:[[mutableAttributedString string] substringWithRange:stringRange]]; 
       return mutableAttributedString; 
      }]; 
     } 
    } 

    [cell setAccessoryView:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chevron"]] autorelease]]; 

    return cell; 
} 

答えて

2

、。ただし、再利用可能なセルのデキューには、 'cellID'という識別子を1つだけ使用しています。したがって、電話するときに:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

セルが常にTTTAttributedLabelであるとは限りません。 indexPath.section == 1の有無に応じてcellIDを変更する必要があります。たとえば、これにより問題が解決される可能性があります。

static NSString *cellID = @"cellID"; 
static NSString *cellIDWithLabel = @"cellID_label"; 
UITableViewCell *cell; 

if(indexPath.section != 1) { 
    cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
} 
else { 
    cell = [tableView dequeueReusableCellWithIdentifier:cellIDWithLabel]; 
} 

関連する問題