cellForRowAtIndexPath: gives incorrect cell bounds in Xcode 9 - ios

Before Xcode 9, I had a working tableview where in cellForRowAtIndexPath: I would check if viewWithTag(1) was nil, as a flag for whether or not this cell needed some initial setup. If so, I'd create some views and add them as subviews to the cell's contentView. I would also position these subviews using the contentView's bounds.
After Xcode 9, the positioning of these subviews are all messed up, and I found that when cellForRowAtIndexPath: is called, the cell's bounds and the cell's contentView's bounds are not what they should be. Their height is 44 points, which I guess is the default row height. But it should be 100, because in my viewDidLoad I call:
self.tableView.rowHeight = 100
I don't know whether this is just a problem I'm experiencing or if its a bug / change in Xcode 9.

Related

heightForRowAtindexPath not called after tableView.beginUpdates in ios 15

Before iOS 15 after I made
self.tableView.beginUpdates()
self.tableView.endUpdates()
method heightForRowAtindexPath was called and my cell height changed without reloading data in the cell. Now in iOS 15 this not works! How can I force reload tableview cell height without reloading data in this cell?
I have already solved the problem. Apparently, Apple made a change in the way updates are processed and cell sizes are adjusted.
As the "hidden" cell is left at 0, when sending the beginUpdate, it only processes the cells that have a value greater than 0, so that is why it no longer adjusts them.
What you need to do is change the value of 0, returned for hidden cells, to 0.01, and you're done!
I hope this solution works the same for me.
Found a solution:
In heightForRowAtindexPath method return for resizable cell UITableView.automaticDimension instead of size
Inside my custom tableView cell I added height constraint to my container view (in my case this is webView)
In a place where I get cell size and need to update height (for me after webView content was loaded), I update height contraint:
.
cell.heightContraint.constant = size
self.tableView.beginUpdates()
self.tableView.endUpdates()
So the cell is autoresizable now with UITableView.automaticDimension height and, of course, your view/container view should have top and bottom constraints equal to cell`s top and bottom constraints

Layout issues in UITableView after updating to Xcode 9

After I updated to Xcode 9, UITableView in iOS 10 device is behaving very strangely. The tableView cell sizes are not maintained. It it resizing by itself and not following the constraints and cell sizes I had explicitly coded. The gap between the tableView cells is black all of a sudden. However, these layout issues are not reciprocated in iOS 11 handset.
In a nutshell, the layout is completely screwed in iOS 10 devices. Is anyone else having backward compatibility after updating to Xcode 9? If so how did you resolve it.
To change the frame width of the custom table View cell cell I am using the frame property. The code I am using is
override var frame: CGRect {
//Decrease frame width of the tableViewCell
get {
return super.frame
}
set (newFrame) {
var frame = newFrame
frame.origin.x += 8
frame.size.width = frame.size.width-16
super.frame = frame
}
}
Below is how the tableViewCell is presented in iOS 11 which is correct
this is how the tableViewCell is presented in iOS 10 which is incorrect
[
As you can see the gap from right margin to the tableView Cell is more.
Throw away your existing code. Don't touch the cell's frame in any way.
To make cells that look narrower than the table, give the cell a clear background and put an opaque view inside the content view that is inset from the cell's bounds (or you could use the cell's background view for this). Use autolayout to configure this, so that it works regardless of screen size.

Autoresize cell xib to tableview cell width and row height

I am currently working with a custom tableviewcell xib and want the size of it to be automatically resized to the tableview's width and it's row's height.
The size of the cell's view does not auto adjusts to the tableview. So I have tried to change the frame inside the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) delegate no changes are reflected (if I print the frame size before and after it does in fact change.)
Does anyone know a solution for this? The best case would be to make the cell's frame auto adjust to the tableview's width and row height.
Edit: After further examination the problem probably has to do with iOS8 Size classes. When I resize the cell inside the xib, it will just expand to a greater width than the table view's size. The label is supposed to be in the center (had set them with constraints and programmatically, got the same results).
Here's a screenshot:
Found the problem, and it was my mistake. I never set constraints to the TableView inside the ViewController, so it maintained the default base size defined by Size Classes introduced in iOS 8.
That is why the width of the cell expanded about the double of the view controller's width.
What i understand from your Question is You want to set cell's width equal to table's width And cell's height equal to tableviewCell 's height!
By default The height of tableviewCell is 44. So you can set Cell's Height to 44! and you get tableview's Width by
self.tableview.frame.size.width
and You can set cell's Frame by cell.frame!
for Example
CGRect cellRect = CGRectMake(0, 0, self.tableview.frame.size.width ,44);
cell.frame = cellRect;
Hope all things Going Well and that may help you!

iOS8 Beta 3 UITableViewCell layoutSubviews infinite loop

There were auto-resizable table view cells introduced in iOS8 (WWDC Session 226 What's new in table and collection views).
In my project I'm trying to implement old fixed rows height behavior. Also, the most important thing for me is to inset default cell.contentView frame with margins on the left and right side.
So, I change the cell.contentView.frame property and immediately after that the -[cell setNeedsLayout] method is being called and and it leads to cell get stuck in an infinite layoutSubviews loop.
Steps to reproduce:
Create new single view project and replace default view controller with the table view controller
Disable table view automatic height calculation
-(void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 0;
self.tableView.rowHeight = 44;
}
3. Drop a custom cell in the table view in storyboard,
Add any subview to the custom cell:,
Subclass the cell and change contentView.frame in layoutSubviews,
Build and run.
Result:
Simulator ends up in a black screen stuck in a infinite layout subviews loop.
Expected Result:
Simulator displaying a table view with a cell's contentView having a custom frame.
Comment:
While debugging a little bit, I found that the infinite loop can be avoided if the cell does not have any custom subviews dropped on it. So it seems the bug will appear after the following conditions are met:
self.tableView.estimatedRowHeight = 0; self.tableView.rowHeight = 44;
cell.contentView.frame = CGRectMake(...)
cell have custom subviews dropped on it in xib or storyboard
Apple does not have the issue in the "Known issues" list for iOS8, so I'm wondering is it actually a bug in iOS8 or does anybody know how to resolve the issue?
This is not a bug: setNeedsLayout will be called any time you change view's frame.
My guess is that changing cell.contentView.frame also changes cell.bounds in iOS 8, triggering relayout. This behavior may be different between iOS versions; anyway, those are standard views, so we shouldn't change them in unsupported ways.
Rather than operating on cell.contentView, how about adding a custom view with insets to it? Or simply creating a height constraint?
In ViewDidLoad please use the below line and there is no need to give the row height
-(void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 44.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}

UICollectionView working on iOS7 but not on iOS6

My UICollectionView cells don't get displayed on iOS6, because my delegate method cellForItemAtIndexPath doesn't get called. I suspect because of this warning:
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the `UICollectionView`
minus the section inset's top and bottom values.
I don't get the warning on iOS7, and all the cells display correctly there too.
I've set my collectionView frame to height 270 in the .xib and there are no insets defined.
I've set my cell height to 270 in the .xib.
I can print out my collectionView frame at runtime and it's 271.
Also, my collectionview is actually inside a custom tableview cell.
Any ideas?
Thanks!
Try to set self.automaticallyAdjustsScrollViewInsets = NO
This was introduced in ios7 so you might want to wrap that with an ios version check, if you are supporting ios6 and below.
This fixed my problem! In my .xib, setting my Collection View Size Cell Size to a smaller value.
My setup is that I have this collectionview inside a custom tableview cell and
I do return the height of my tableview cell programatically (depending on the content). So it could be that my warnings had to do with my collectionview not fitting inside the tableview cell. So setting the initial collectionview to a smaller value fixed it.
I was on the wrong path thinking that the problem was with my collectionview and its colletionview cell.
Maybe?
self.automaticallyAdjustsScrollViewInsets = NO
actually did the trick. it also resolved my issue in swift, where the cells of a horizontal flow layout had a frame top of -32 (???) and did not fit into the collection view properly.
I found that I had to manually set self.collectionView.collectionViewLayout.itemSize in viewWillLayoutSubviews.
- (void)viewWillLayoutSubviews {
self.collectionView.collectionViewLayout.itemSize = CGRectMake(...);
}
Another possibility to generate the same trick would be to implement the method
collectionView:layout:sizeForItemAtIndexPath:
I have the same issue, in my case the size of collectionCell in storyboard is 96x96 and also under -(CGSize)collectionView:layout:sizeForItemAtIndexPath:
the solution was removing this delegate:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets insets = {.left = 10, .right = 10, .top = 5, .bottom = 5};
return insets;
}
And by the way this is under ios7, it's late but hope this will help some.. Cheers..
Set:
self.itemSize = CGSizeMake(1, 1);

Resources