Incorrectly positioned UITableViewCell when containing UITextField - ios

I've created a simple project. It contains a UITableViewController with self-sizing prototype cells that contain a UITextField and a UILabel for displaying validation message. The controls are grouped in a UIStackView, but that doesn't seem to matter.
The label is only visible if there is a message to display, otherwise it's size collapses to 0. To adjust the size of the cell I simple call the recommended:
tableView.beginUpdates()
tableView.endUpdates()
If I scroll the view with the UITextfield remaining first responder (ie w/o dismissing the keyboard), so that the cell is no longer visible, when scrolling back to the cell, it is misaligned.
I've hosted the sample project at https://github.com/ionel71089/tableViewTextField
Note:
Easiest way to reproduce is on Simulator iPhone 6 9.2, by editing let's say the 50th cell, scrolling to the top and then back to the cell.

Related

How do TextFields behave as FirstResponder when placed inside TableViewCells and scrolled out of view?

Assume we have UITableView holding a large number of custom UITableViewCells which contain UITextFields and/or UITextViews.
We randomly tab on any cell an the text control inside becomes the FirstResponder. The keyboard appears and we can type some text. Everything is fine.
Is there any reliable information on what happens with the TextField when the cell is re-used for another cell?
In tableView.cellForRowAt:indexPath the cells are dequeued using dequeueReusableCell. Is it my responsibility to keep track on which cell contained the FirstResponder and resign / reset it when the cell is scrolled out of view / back into view as I do for my custom view model?
I ran different test and it seems that this is handled automatically somehow. However, I would like to be sure. Could be coincidence that I did not ran into any problems in my tests.

Only first collectionViewCell reacts to user interactions

The setup
I have a rather complex view hierarchy which made me do terrible things with tableViews and collectionViews. Right now I'm having a regular grouped TableView with custom TableCells. TableCell itself contains a couple of views and a Collection View. The height of the table cell is calculated based on number of items in the data source. On its creation table cell creates a collection view with a calculated size to fit necessary data.
-- UITableView
---- UITableViewCell
------ UICollectionView
--------- UICollectionViewCell
The problem
I've encountered unusual problem with a custom collectionViewCell. I have a vertical single-column collectionView with dynamic amount of cells. Ideally tapping on the cell should call didSelectItemAt. The cell also has three buttons. Tapping on the button should trigger some action. All of the desired functions work only for a first cell. The rest of the cells are not responsive to any actions.
Things that look strange
By default the scrolling of a collectionView inside of the tableViewCell is disabled because it basically fits all the content based on calculated height and doesn't require scrolling. (Also I don't want it to interfere with tableView scrolling logic).
First
I've tried to hardcode some value for the height of collectionView and enable scrolling. What happened is a mystery for me.
Let's say that calculated height required for the collectionView to show all the content without scrolling is 740. When I manually set it to be 280 (this is enough for exactly 2 cells to fit) and enabled scrolling my first cell were still working, but also when I scrolled collection just a little bit my second cell started to act normally as well. When I scrolled back to the top of the collectionView it was disabled once again.
So it looks like when the scrolling is enabled and will actually occur because of insufficient height to fit the content, cells behave as they should. As soon as I set height of the collectionView to be enough to fit its content, things go wrong.
Second
In some cases I can actually tap on the second cell and it will call the delegate. But the weird thing is it works when I tap in the top area of the cell, like 10pts from the top. The other areas of the cell are unresponsive so are the rest of the cells in the collection.
The working delegates and buttons with enabled scrolling forces to think that this has nothing to do with delayed or canceled touches. The frame for collectionView and height of the table cell are calculated properly as well.
xCode 8.3, iOS9+
As it turned out when I was creating my tableViewCell, I hardcoded the height of the collection to value of 214. The reason for that is very simple. I create a cell programmatically and with tableViewCells created programmatically the height of the cell is always 44. When you override init(style: UITableViewCellStyle, reuseIdentifier: String?)
You need to either hardcore the height, or update it in cellForRowAtIndexPath. Even though I updated collection frame when it was filled with data, the container view of this collection wasn't updated resulting in some sort of clipping area for user interactions.

Accessibility not working properly on UICollectionView inside UITableViewCell

in my app i have a UICollectionView and UIPageControl inside UITableViewCell. UICollectionView has three different kind of cells and each cell covers the full width of iPhone. With accessibility on we are facing a problem when the focus is on UIPageControl and we try to adjust its value by swiping up and down and then traverse back to the cell it loses its focus and takes us to some different cell instead of taking control to the cell which we have changed using UIPageControl.
We are changing the cell on value change event of UIPageControl.

How to scroll or prevent scrolling in UICollectionView using swift?

I have a UICollectionView with one section, 10 cells and horizontal scrolling using a Flow Layout.
One cell occupies the entire screen, and has a UITextView and two UIButtons.
How can I prevent scrolling if no button in the cell is selected, and if it is selected, how can I scroll to the next cell on a UIBarButton click?
I would also like to preserve selection in order to be able to go back to the previous cell and change the selection if needed.

Custom UITableViewCell layer cutoff

I have a UITableView with custom cells. In the awakeFromNib of the cell custom class I add the cell labels etc. using AutoLayout. They display fine when viewed first time.
(Blue rectangle is a single UITableViewCell)
The problem is that after I swipe the table view so that some cells goes offscreen then back again (i.e. cells are re-layout) the cell layer gets cutoff.
Another way to debug is to go to Xcode, Click Debug>View Debugging>Capture View Hierarchy on Xcode 6+. It looks like there is possibly an unused view being added to the bottom left corner of your view? Try the view debugging to see if that's the case.

Resources