How to redraw custom collection layout when keyboard is visible? - ios

I have custom collection layout to draw cells using uicollectionview. Each collection view cells have input text view. When user enters text need to resize cell height.So I call collection view invalidate layout. But it redraws all sections and rows in prepare layout and keyboard dismissed automatically. I want to keyboard stay up until user taps close button. When user typing text need to increase height of cell height.
Why I go for custom layout is my app needs row span and col span options (Merge and unmerge cells) in uicollectionview. This can't be implemented in normal uicollectionviewflowlayout. Can anyone help me to advise this issue?

About redraws
I have a custom layout in my current app. It consists of all kinds of cells including cells with text fields. I start editing the cell with field, keyboard appears. Then I press the button on another visible wishlist cell which adds the product to wishlist. When product is added I invalidate the layout and the wishlist cell is reloaded. During all these manupulations the field cell remains first responder and the keyboard goes nowhere.
But it redraws all sections and rows in prepare layout and keyboard dismissed automatically
Are you sure that you are not calling the reloadData()? It's not the layout who redraws everything, it's usually the developer who calls reloadData().
I want to keyboard stay up until user taps close button.
So, if you switch from reloadData() to batch updates - the keyboard will stay.
About resize
When you invalidate your layout you prepare new attributes for cells. Attributes with new sizes/frames. But the data source is the same. It means that UICollectionView will just move the cells around but they won't be reloaded. You don't have to reload anything! Just invalidate and provide new attributes. The field cell will automatically grow.
Example
In About redraws I described the way I reload the wishlish cell. Also, I added field attributes size change. You can now see that the keyboard remains and the field cell size changes. It now looks this way:
So, I believe, you just have to work on your layout. It is possible to implement the desired behavior. Good luck and happy coding (:

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.

UiCollectionView containing TextFields inside UitableView

I have a table of 10*10 which i have to adjust in my main tableview which contains other data as well. So i decided to put collection view inside my main table view cell to minimise the scrolling.
Everything has went well except when i try to enter data in the text fields of collectionview. As in this case keyboard appear and collectionview adjust it self by moving upward then when they keyboard disappear it never comes back.
The collection view moves up when i put some data in the textfield and try to horizontally scroll it.
Any help please.

How to add extra table view cells to handle user input?

Recently I have been trying to create a table view with different table view cells. What I want to do is that when users click on each table view cell, it shows an extra cell underneath the selected cell to handle user inputs and the extra section disappears when the cell is unselected.
I am fairly new to iOS development and I am wondering what would be the best way to achieve this. At the moment I am thinking of hiding the extra cells initially and displaying each of them when the cell above is selected.
Any help would be appreciated.
Apple has a great set of sample code that demonstrates the behavior you're looking for — displaying a cell beneath another cell when selected. This behavior is used in Calendar when displaying a date picker, and it's pretty much what you've described.
Question: Will each cell have an identical set of options?
If so, I'd consider including the user inputs as part of the source cell and adjusting the height of the source on selection. You can animate the cell's height changing using tableView's beginUpdates and endUpdates. This way, you avoid messing around with cell indices.

Reloading UITableView resets view's subviews

my view controller has a UITableView and my app is a chatting app where the table loads new chats. so i added a textfield that when pressed it goes over the keyboard so the user can see it. now when the textfield has been moved upwards and a new chat is available and the table view reloads, the textfield drops down to it's original place in which it was positioned and the keyboard is still in it's place as the textfield didn't resign it. How can i prevent the subviews of my view to reset to their original settings when i reload the table view?
Edit: the textfield isn't added to a cell. The view has a UITableView that i resized to keep space for the textfield in the bottom.
Unless you have done it already I suggest to have a regular view with a regular UIViewController as "host". Within that you have the textfiled and what ever else may be needed. And you have a container view where the Table View should reside. Within that container you nest your UITableView and its controler. By doing so reloading and other table manipulation tasks (like adding or deleting cells ect) cannot influence the layout of the hosting view.
If your app is a chatting app, I suppose you have your answering field at the end of your UITableView.
So just add a footer to it, containing your field.
Resizing the footer shouldn't mess up your all cells.
tableView.tableFooterView = ...
Then you don't need to resize your table since the field is part of it (it's in the scrollview of your tableview)

UICollectionView change layout - change cells

I have a UICollectionView which uses UICollectionViewDelegateFlowLayout. I've added a method which changes the layout when tapping a cell setCollectionViewLayout.
All the cells have a text label with text and I would like to empty all the text labels on every cell when changing the layout. What's the best way to accomplish that?
Thanks,
I would imagine the simplest way is just to call reloadData on your collection view at the same time that you change the layout (and have a conditional in your cellForItem data source method that empties the text labels upon each layout change).
Alternatively, you could have your UICollectionViewCells keep track of the layout they're currently displayed in, and adjust their views accordingly.

Resources