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.
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 :)
Related
I have UIView in my tableview custom cell, when it appears i want to change the position of the the UIView based on certain condition.
i put this code in method cellForRowAtIndexPath:
CGRect newFrame = CGRectMake(11, 306, 302, 95);
[cell.myView setFrame:newFrame];
[cell setNeedsLayout];
I'm certain that the view already have the new position based on NSLog.
But it still in the same position in my screen.
Is there any steps that I missed?
The frame you are assigning based on certain condition, needs to be assigned inside LayoutSubviews method of UITableViewCell Class.
I created a dynamic cell using AutoLayout, put code in (void)updateConstraints cell's subview method, set BOOL value so custom AutoLayout code runs only once, and in View Controller call
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
just before returning cell. Everything seems to works fine but I'm experiencing weird issue, when I select cell all its subviews (or itself?) change position.
cell pointed with this beautiful arrow shows it ;]
I experienced the same bug and tinkered for about a day to fix this ... to get to the bottom of the issue I set different background colors on the cell & the content view. Right after first showing the table view everything seemed to work, but after selecting the cell the contentView would jump around - sometimes when selecting, sometimes when deselecting (which I do immediately in tableView:didSelectRowAtIndexPath:), and the cell's background became visible. So I figured the cell somehow didn't reestablish the correct contentView dimensions.
Finally it turned out that at least in iOS 8 you need to set the appropriate autoresizingMasks for the cell as well as the contentView, and then make the layout system translate them into constraints by setting translatesAutoresizingMaskIntoConstraints to YES. At the same time this flag needs to be NO on all subviews of your contentView.
So here's my initialisation for custom cells:
- (void)awakeFromNib {
// enable auto-resizing contentView
[self setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.contentView setTranslatesAutoresizingMaskIntoConstraints:YES];
self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// remove autoresizing constraints from subviews
for (UIView *view in [self.contentView subviews]) {
view.translatesAutoresizingMaskIntoConstraints = NO;
}
}
Works like a charm for me. For the record, I'm using the UITableViewCell auto-sizing which was introduced in iOS 8, like this:
// enable automatic row heights in your UITableViewController subclass
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80.0; // set to whatever your "average" cell height is
I found solution to this issue,I used instructions under this link dynamic-cell-layouts-variable-row-heights to implement dynamic table view cells: [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
[1]: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights. Author suggests to use this two methods to update constrains when using updateConstraints method inside cell's subview.
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
but what I found in my case is there was some issue that not all cells was updated as they should. Solution is to call [cell updateConstraints] in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and not wait until system will update constrains.
I have a UITableViewCell which contains an UIView.
This UIView is larger than the UITableViewCell and should also be visible in the cell above.
clipsToBounds=NO
did not work.
This does only work when I remove the content from the upper cell and set the background color to clear.
This means, the content in my upper cell covers my UIView, that's why I tried to call bringSubViewToFront, but this did also not work.
What am I doing wrong?
This is how I add the custom UIView to the cell's contentView:
CustomView *view=[[CustomView alloc] initWithFrame:CGRectMake(0, 0, [DeviceInformation getWidth], [self tableView:tableView heightForRowAtIndexPath:indexPath])];
[cell.contentView addSubview:view];
and in my CustomView I am adding another UIView, which should be then presented among multiple Cells.
Thanks!
I have a tableview with my custom class for UITableViewCell, I have added some subviews to each of the UITableViewCell, now when I enable editing like this
[tableView_ setEditing:YES animated:YES];
a red delete button appears on left side of each cell and each cell is shifted towards right except the subviews which are added on the cell. This causes the button to appear on top of subviews. How to fix this? Thanks
UITableViewCell have a specific view for it's content, cell.contentView, make sure all of your addSubview calls are to this view not the cell. EX:
// good
UILabel* label = [[UILabel alloc] init];
[cell.contentView addSubView:label];
// bad
[cell addSubView:label];
I wish to have a table with some label and then I add a text field UI after the label.
I want the Label to be of some fixed size (say 150) and the rest of the cell width be occupied by the textfield.
Hence I set the frame of my cell.textLabel in function
cellForRowAtIndexPath
in this way
[cell.textLabel setFrame:CGRectMake(cell.textLabel.frame.origin.x, cell.textLabel.frame.origin.y, 150, cell.textLabel.frame.size.height)];
but this doesnt seem to work.
Any ideas why and What could be my solution to this?
why don't you add custom lable on your cell...
UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5, 150, 40)];
[cell.contentView addSubview:mylabel];
Otherwise you need to override -LayoutSubViews method and have to subclass UITableViewCell..
With a UITableViewCell its responsible for laying out its subviews. You have a very high level control over its layout using the UITableViewCellStyle enum.
If you want something custom, override UITableViewCell and add view to its content view or modify existing views. When you do this, be sure to do the layout in the layoutSubviews method, e.g.
- (void)layoutSubviews
{
[super layoutSubviews];
self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x,
self.textLabel.frame.origin.y,
150,
self.textLabel.frame.size.height);
}
The cell's frame will change when its added to the UITableView. When the cell's frame changes layoutSubviews is called to give you a chance to adjust its subviews. By default the UITableViewCell will do some if its own adjustments which is what was probably causing your issue.