JamEngulfer
JamEngulfer

Reputation: 747

UITableViewCell not responding to setNeedsLayout

I have a UITableView in Grouped format with a few cells in it. In the cells is a UIView to which I am adding another UIView. When I update the frame of the first UIView, it doesn't change the position of it nor the size.

The UIViews realign themselves correctly if the cell goes offscreen and onscreen again, so the actual transformation is being applied, it's just the re-rendering that's not happening properly.

The code for this is just in my cellForRowAtIndexPath:

[cell.cellMoney setBackgroundColor:[UIColor clearColor]];

[cell.cellMoney.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView *sellMoneySubView = [UIMoneyDisplay createViewWithAmount:item.min_sale_unit_price fontSize:17.0];
[cell.cellMoney setFrame:CGRectMake(cell.frame.size.width-sellMoneySubView.frame.size.width-20, 7, sellMoneySubView.frame.size.width, sellMoneySubView.frame.size.height)];

[cell.cellMoney addSubview:sellMoneySubView];

[cell setNeedsLayout];

The sub UIView that I'm adding to the first UIView gets added and rendered properly, it's just the first's positioning that's gone weird.

Oh, I'm also using AutoLayout with the storyboard.

Addition: I fixed part of my problem, but the title is still valid. I also have a cell with an image that needs to be downloaded through the network, but that cell doesn't respond to any form of layout call either.

I fixed the problem with the position of the UIView by removing the middle view and adding straight to the cell's contentView.

Upvotes: 1

Views: 1627

Answers (1)

Yuyutsu
Yuyutsu

Reputation: 2527

instead of this [cell setNeedsLayout] use [cell layoutIfNeeded]

[cell.cellMoney setBackgroundColor:[UIColor clearColor]];

[cell.cellMoney.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView *sellMoneySubView = [UIMoneyDisplay createViewWithAmount:item.min_sale_unit_price fontSize:17.0];
[cell.cellMoney setFrame:CGRectMake(cell.frame.size.width-sellMoneySubView.frame.size.width-20, 7, sellMoneySubView.frame.size.width, sellMoneySubView.frame.size.height)];

[cell.cellMoney addSubview:sellMoneySubView];

[cell layoutIfNeeded]; //use this
return cell;

This might helps you :)

Upvotes: 1

Related Questions