I display different types of contents in a tableview
and calculate the height of each cell using different custom methods, in heightForRowAtIndexPath
.
One of these custom methods implies converting some html in an NSMutableAttributedString
, and then calculating the height of this NSMutableAttributedString
.
For html conversion I use the new initWithData:
method.
All works perfectly except when I rotate the screen => I've got an exc_bad_access every time.
Using Instruments / Zombies, I've been able lo locate the error, and in fact it's this initWithData:
.
(When I remove this method and create a "simple" NSMutableAttributedString
with initWithString
, I can change orientation as many time as I want, no crash
anymore).
Any idea why?
(By the way, my project use ARC)
Instrument / Zombie screenshot :
Custom method called in heightForRowAtIndexPath
:
< UtilitiesForFrontEndUI heightForFacebookAttributedText
: >
+(CGFloat)heightForFacebookAttributedText:(NSString *)attributedText withWidth:(CGFloat)width
{
NSAttributedString *formatedText = [self formatRawFacebookContentForFrontEndRichTextContents:attributedText];
CGRect rect= [formatedText boundingRectWithSize:CGSizeMake(width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
return ceilf(rect.size.height);
}
Custom method using the initWithData for html to NSMutableAttributedString
conversion :
< UtilitiesForFrontEndUI formatRawFacebookContentForFrontEndRichTextContents:
>
+(NSAttributedString *)formatRawFacebookContentForFrontEndRichTextContents:(NSString *)stringToFormat
{
// THIS GENERATE EXC_BAD_ACCESS ON DEVICE ROTATION (WORKS IF NO ROTATION)
NSData *dataContent = [stringToFormat dataUsingEncoding:NSUTF8StringEncoding];
NSMutableAttributedString *richTxtContent = [[NSMutableAttributedString alloc] initWithData:dataContent options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];
NSRange myRange;
myRange.location = 0;
myRange.length = richTxtContent.length;
[richTxtContent addAttributes:[self commonAttributesForFrontEndRichText] range:myRange];
return richTxtContent;
}
If I replace initWithData by a simple initWithString no more exc_bad_access
+(NSAttributedString *)formatRawFacebookContentForFrontEndRichTextContents:(NSString *)stringToFormat
{
// THIS WORKS (NO MORE ROTATION CRASH)
NSMutableAttributedString *richTxtContent = [[NSMutableAttributedString alloc]initWithString:stringToFormat];
NSRange myRange;
myRange.location = 0;
myRange.length = richTxtContent.length;
[richTxtContent addAttributes:[self commonAttributesForFrontEndRichText] range:myRange];
return richTxtContent;
}