Re-calculate height or a UITableViewCell after image is loaded? - ios

guys:
I created table view cell with the cellForRowAtIndexPath delegation method, and now I want to load some photos into the cell and resize the height of the cell to fit those photos, so I think I should do this after the photos are all loaded so that I can get the height to resize the cell.
I know the height of a cell will be calculated within the heightForRowAtIndexPath delegation method, but the heightForRowAtIndexPath method seems to be call before cellForRowAtIndexPath?
so, is there any way to re-calculate the height to resize or re-create the cell after the photos are loaded?
thanks :)

The easiest way is reload the cell:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationNone];
We do that in one of our apps, and it even animates nicely.

Related

UICollectionView display previous cell content in next cell while scroll horizontally

I am using a UICollectionView to show an array of images, horizontally and zoom in/out that image in the cell with the help of scroll view using constraints. But when I scroll horizontally it shows me previous cell content in the current cell. I am and not able to identify what is the problem.
CollectionView datasources
CollectionCell Class
try this when you reuse the Cell in cellForItem at
cell.imageView.Image = nil
Please share some code so that we can help you better. Meanwhile, make sure you have the following parts working correctly in your code:
Register correct UICollectionViewCell subclass with correct reuse identifier
Use the method [self.collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath] and pass both the parameters correctly.
In UICollectionViewCell subclass, override prepareForReuse and reset the UI parameters there.
Override prepareForReuse in imageCell class, and set imageView.image to nil.
The contentSize of the scrollView should be as big as imageView.image.size, or just use MWPhotoBrowser

Empty UICollectionView inside UITableViewCell

I have used this tutorial to put a collectionView inside a UITableView. https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
The UITableViewController is the dataSource & delegate for both the UITableView & the different collection views in each cell.
The problem is that I want to dynamically hide the CollectionView and to change its height to 0 whenever the collectionview is empty.
To do so, I have this code in CellForRowAtIndexPath
if (patients.paraclinicImage.count == 0){
[cell.collectionView setHidden:true];
cell.collectionViewHeight.constant = 0;
} else {
[cell.collectionView setHidden:false];
cell.collectionViewHeight.constant = 80;
}
By having this code, the collectionView shows up correctly initially. However, when scrolling fast, I will sometime have the cell load with the correct cell height, however the collection view will be empty. Refreshing the cell fixes this issue. Removing the above line of codes also fixes the issue.
Here are two images showing how the cell looks when first rendered, and after multiple scrollings (and re-renderings of the cell).
Before scrolling issue looks like below image
But After scrolling issue seems to like below one
I would appreciate any ideas you guys might have.
Debug view hierarchy showing an empty collectionView with an appropriate cell height
It is because your cell height is not updated on fast scrolling. UITableView caches the row height of indexPath just to calculate the scroll area. You need to set constraints properly and adjust the height of cell in heightForRowAtIndexPath: method.
Apply the logic inside heightForRowAtIndexPath: and your problem will be solved.
Happy Coding!!
I had the same issue. Sometimes, my collection view inside a tableviewcell wouldn't show the collectionview cells. I solved it by calling collectionView reload on main thread. I don't exactly know the reason, as I have many background calls on each cell and was reloading the tableview on main thread itself. Still, had to reload collectionview on main thread.
DispatchQueue.main.async {
cell.colViewProperties.reloadData()
}

Reload UITableViewCell without UITableView scrolling

I'm using a UISegmentedControl which changes the contents of a UITableViewCell. After the segment changes I need to reload the cell to update the height of it. I do this by calling:
[self.tableView reloadRowsAtIndexPaths:#[self.segmentIndexPath] withRowAnimation:UITableViewRowAnimationNone];
However, this makes the UITableView scroll to the top. What is the best way to do this without having the table scroll?
Try to use [self.tableView visibleCells] and then find this cell and change

UITableViewCell - Update tableview layout

I have a tableviewcell, that on tap, grows in size, height wise, by updating the frame.
The problem is, the cells below don't adjust, move down to make it visible underneath, and the select row events are still based on the old sizes, before tap. I am using Facebook POP - which is handling animation, so tableview.beginUpdates() is out of the question, maybe?
You cannot manually update the frame of a UITableViewCell by changing its frame or bounds. Instead, you need to change the value returned by -tableView:heightForRowAtIndexPath: for that indexPath and then perform:
[self.tableView beginUpdates];
[self.tableView endUpdates];
This will cause the tableView to recalculate the heights of all the rows.
First, you need to make sure that you return the new height in heightForRowAtIndexPath, then you need to make the tableview update the cell. If you don't care about animation just call [tableview reloadData]. If you want animation you need to call [tableview reloadRowAtIndexPath: indexpath_of_your_cell]

UITableView - how to dynamically update frame of self-resizing cells

I successfully implemented the self-resizing cells with autolayout. I added a UITextField to my content view that takes a string from the model, and the cell is now resizing correctly according to the length of the string.
The user is supposed to be able to edit this text field - how do I update the frame of the cell on user input (as the text field grows)?
I could resize the cells and the rest of the table view manually, but I figured there might be a better and simpler way to invalidate and refresh the frame of the cell that is being edited?
I want the frame changes to animate smoothly (e.g. as the textfield text requires a new line, this cell grows in height, and the cells below are pushed down accordingly).
If you just want to refresh a single cell you can do the following:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForItem:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Obviously you should change the indexpath to the correct one for your cell.
If you do this in something like textViewDidBeginEditing after checking if the contentSize of the UITextView has changed you should get the effect you're after.

Resources