Quantcast
Channel: setNeedsLayout relayouts the cell only after is has been displayed - Stack Overflow
Viewing all articles
Browse latest Browse all 3

setNeedsLayout relayouts the cell only after is has been displayed

$
0
0

I have a table view with cells, which sometimes have an optional UI element, and sometimes it has to be removed.Depending on the element, label is resized.

When cell is initialised, it is narrower than it will be later on. When I set data into the label, this code is called from cellForRowAtIndexPath:

if (someFlag) {    // This causes layout to be invalidated    [flagIcon removeFromSuperview];    [cell setNeedsLayout];}

After that, cell is returned to the table view, and it is displayed. However, the text label at that point has adjusted its width, but not height. Height gets adjusted after a second or so, and the jerk is clearly visible when all cells are already displayed.

Important note, this is only during initial creation of the first few cells. Once they are reused, all is fine, as optional view is removed and label is already sized correctly form previous usages.

Why isn't cell re-layouted fully after setNeedsLayout but before it has been displayed? Shouldn't UIKit check invalid layouts before display?

If I do

if (someFlag) {    [flagIcon removeFromSuperview];    [cell layoutIfNeeded];}

all gets adjusted at once, but it seems like an incorrect way to write code, I feel I am missing something else.


Some more code on how cell is created:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    ProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];    [cell setData:model.items[indexPath.row] forMyself:YES];    return cell;}// And in ProfileCell:- (void)setData:(Entity *)data forMyself:(BOOL)forMe{    self.entity = data;    [self.problematicLabel setText:data.attributedBody];    // Set data in other subviews as well    if (forMe) {        // This causes layouts to be invalidated, and problematicLabel should resize        [self.reportButton removeFromSuperview];        [self layoutIfNeeded];    }}

Also, if it matters, in storyboard cell looks like this, with optional constraint taking over once flag icon is removed:Constraints in the storyboard


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images