Search code examples
iosobjective-ctableviewnsattributedstring

NSAttributedString not scrolling smoothly (disappearing/reappearing) in tableViewCell


I have a tableview cell, and within it, I have a button. Within this button, I am displaying an attributed text, which is dynamic. This works, however, when I scroll up and down, the text disappears and appears a quarter of a second later, and it is quite noticeable, especially when scrolling fast.

When debugging, I noticed if I commented out the lines attString = [[NSMutableAttributed... until the last [attString appendAttributedString:[[... method (below my comment), and set the title of the profileIDButton to @"test", there was no issue when scrolling. I believe that the allocation/initialization of attString is causing the scrolling issues. How do I rid this disappearing and appearing a quarter of a second later behavior? Is there any way to create an attributed string and append to it without allocating/initializing memory in the cellForRowAtIndexPath?

//Within the cellForRowAtIndexPath method:

    //  HERE IS WHERE I BELIEVE IS THE CAUSE OF TEXT DISAPPEARING DUE TO MEMORY ALLOCATION.
    attString = [[NSMutableAttributedString alloc] init];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"@"  attributes:dict1]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:senderName   attributes:dict1]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@" posted "      attributes:dict2]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:recepientName      attributes:dict2]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"."      attributes:dict2]];

    [cell.profileIDButton setAttributedTitle:attString forState:UIControlStateNormal];

Top cells displaying this refreshing behavior when scrolling up
enter image description here


Solution

  • Set the button type to 'UIButtonTypeCustom' maybe work.