UITableViewCell with dynamically changing the heigh and adding new components? - ios

I have a UITableViewCell which has 50px of height and I would like to change this height to 300px and to append one more view insight the new height. I did a project which you can download and test with all the code and functionality (Link to the project on DropBox). The main problem is that when the cell (random cell) is selected the height is changing correctly but the new view (with brown color) is not visible although I can print its frame, type and so on (it's not nil neither hidden). It works well if I select the first cell from the beginning and then all the rest cells work correctly. So it's something with the reusing of the cell but I couldn't find the exact solution. Thank you!

Related

UICollectionViewCell loaded from Xib (Nib) is being covered by storyboard cells as its size is not updating

I'm using a Xib file to hold a UICollectionViewCell item that I do not want to have in a Storyboard. I have the UICollectionView set to calculate its cell sizes automatically. But what I see when the collection view loads is that it's retaining the default 50x50 size for the cell that's being loaded from the Xib file. It's not being updated to the size it is showing up as.
The cell looks correct on screen and at the correct size. But when another cell loads that is contained in the storyboard it's loading on top of the other cell as its being placed 50 points down from the first cell.
The cell outlined in orange is the one being covered up by the one in green. I cannot show a larger portion of this due to work restrictions but I hope this clarifies what's happening.
I've tried to say that the cellNeedsLayout() and layoutIfNeeded() but neither of those make any difference. It's like the actual height that the cell is not being calculated early enough to have the collection view place cells around it correctly.
I've not been able to find a way to preload the Xib so that it already knows how large that needs to be. I am using a subclass of UICollectionView named CollectionCell.
Can anyone point me in the direction to look or suggest a fix for this other than moving what I've done into the Storyboard?

Multiline label in UIView vs UITableViewCell

I created a custom UITableViewCell with two labels and a button.
It behaved perfectly as expected, and looked like this:
Later, I realised I needed to use this particular view multiple places, one of which was not in a TableView. Therefore I decided to delete my cell, and create a custom UIView instead - and create an empty custom UITableViewCell only containing this custom view - constrained to 0 in all directions.
With the exact same elements and constraints, the UIView turned out like this when shown in a TableView as a cell:
Everything is identical. NumberOfLines is set to 0, all the constraints are the same.
I also observed something weird, that when I rotated the entire phone to landscape, it turned out like this:
This is exactly how I'd like it to look when in landscape.
If I now turn it back to portrait, suddenly it looks like this:
Now it suddenly looks exactly like I want it to look in portrait.. I just had to flip it to landscape and back to portrait.. Why is that?..
I played around with some variables, among them: preferredMaxLayoutWidth, and found that if I set this value to the size of the screen (minus the 16*2px padding) it looks as expected at first launch in portrait. However, I don't want to set this value. There is no magic number in px that will be right. The whole point of this is to get it fit properly on all screen sizes and orientations. I probably could re-set the preferred width every time superview's layoutSubviews or something is called, but I figured there has to be a solution for this..? Why did it work for UITableViewCell and not for UIView inside a UITableViewCell?
I have tried all sorts of sizeToFit etc., and don't find any questions or answers that targets this particular difference in UIView and UITableViewCell..
The problem here is a bit complex but I will try to explain it... When the view lays out, it initially does so independently of its container and then reports its height to the system when the system needs to determine the height of the cell, which happens before the cell is constructed. Once the cell exists and the device is rotated, the view "realizes" it's in a cell and constraints itself and reports its height appropriately.
Sadly, this is an issue in the table view system stemming from legacy issues with how cells were originally sized back before constraints existed.
The simplest solution is to put the labels and button back directly in the cell. Another option might be to make your external view a table view cell. You can instantiate a table view cell outside of a table view for that one spot it's needed in. Another option is to implement the table view delegate's tableView:heightForRowAtIndexPath: method and manually determine the size.
I am assuming that you are using following method for table view's height:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
So, Just add all your view's in stack and set top, bottom, leading and trailing constraints of UIStackView with respect to UIView of xib as:

Cannot change custom UICollectionViewCell in Storyboard

I have a UICollectionView with custom UICollectionViewCells. I used Interface Builder (via XCode 8) to size the collection view and cells when first making it.
Now, I'm trying to adjust the size of the cells and I'm unable to do so in Interface Builder (using XCode 9). I cannot adjust the width or height of the cells on either the collection view itself or on the custom cell. The up/down arrows don't do anything and neither does typing in a value. It just stays set at 145.
I saw some answers about not being able to do this with a UICollectionViewController via Interface Builder, only a UICollectionView, but it is a collection view and not UICollectionViewController.
I'm sure there's some new setting/config in XCode 9 that I'm missing, but I can't figure out why I can't adjust the custom value.
I ran into the same problem.
Turned out that the item size was defined by the contained flow layout. So editing the item size in the flow layout, and the parent collection view had its item size changed accordingly.
I tried Jonny's answer, but I use a custom FlowLayout and couldn't edit it's cell size either. I had to manually edit the size of the collection view, the cell and the layout in the storyboard source code (right-click on the storyboard in project view and select "open as / source code").
Now the size is correct but still not editable from the UI ¯_(ツ)_/¯
Same problem, width would not change. But I changed the height to something different and it then let me edit the width.
Xcode 12.3. I had the same issue and what fixed it for me was changing the collection view scroll direction from vertical to horizontal to make the change, then obviously reverting back to vertical (my intended direction) once I was done editing the values.

UITableView async image dynamic height jumping

I'm currently working on a project where I need to display a Facebook like feed with (depending on uploaded or not) a imageview and multiline label.
I already created a custom UITableViewCell, and the text content is showing correctly and changing the height of the cell with autolayout. But when a image is loaded into the imageview (with dynamic height), the tableview starts to jump, probably because the cell sizes are changing. I'm using SDWebImage to load the images.
Is there any way to fix this issue?
SOLUTION:
What we did is pre-load the images. Then build the tableview and cells.
Below my custom cell layout in the interface builder, the small stroke on the bottom is the dynamic image:
check this. They have explained everything in detail. Hope this helps

UICollectionView: Reordering cells of varying, but fixed widths

I am trying to build a UICollectionView that consists of cells that are of fixed height and varying widths. The cells can either be 100%, 50%, or 25% in width.
I am creating these cells from a custom subclass, and then I am adjusting the size in sizeForIndexPath. My problem is that when reordering cells, I get a lot of weird visual glitches as the cells have their sizes adjusted rapidly as the index path changes.
Basically, I want the size of the cell to depend on the type of cell (or the content in the cell), not the index path of the cell.
What is the best way to handle this? I thought about using multiple classes for the different types of cells, but I can't set the width of the cell with a frame when initializing it. Any ideas?
Update: Here's a link to a video showing the bug I am trying to solve
http://d.pr/v/FTM8+
Try this tutorial. It for a table view but in the conclusion some recomendations for a collection view. I cannot try this tutorial now and garaunted it works for collection view but tomorrow I will do it.
Cheers.

Resources