Reloading UITableView resets view's subviews - ios

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)

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.

How to redraw custom collection layout when keyboard is visible?

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 (:

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.

UITableView stops scrolling enough after I add/remove a view to the superview of table view

Here is the problem that I'm facing. I have UITableView inside main view. When user taps menu button, view is added to the view hierarchy of superview, and this view covers all screen, except the menu button. That is I call
[self.view insertSubview:self._menuView belowSubview:self._menuButton]
After user clicks the menu button again I call
[self._menuView removeFromSuperview]
However, after this, the UITableView stops scrolling, actually it scrolls, but just a little, couple of pixels, and then bounces back to the location on which it was before I inserted _menuView. I've monitored the menu hierarchy after inserting and removing the _menuView, it is actually removed, and also there is no view which covers the UITableVIew, so there is no chance that some other view steals touches from UITableView. What can be the problem? Any ideas?
EDIT 1: I've noticed besides not being able to scroll, another strange thing happens. When I show menuView, the tableView is scrolled to top, without animation. (the menuView is half opaque only, so I can see the tableView underneath it)
EDIT 2: I've monitored contentSize and contentOffset of tableView before and after inserting _menuView, the reason for not being able to scroll is that somehow contentSize is being changed.
TABLE VIEW BEFORE INSERTING MENU - contentSize:{Width=320, Height=10519} contentOffset:{X=0, Y=3980}
TABLE VIEW AFTER INSERTING MENU - contentSize:{Width=320, Height=44} contentOffset:{X=0, Y=0}
As You can see, the height is changed to 44 that's why I'm not able to scroll. And Also, as I've said in EDIT 1: tableView is scrolled to top - so as you can see contentOffset is also zeroed. Any ideas why content size changes automatically?
Thanks in advance.
i think there is problem with your tableview frame. right after removing subview, try to set frame of your tableview at its original frame.may this will solve your problem
Content in the UITableView organized automaically and designed only to show cells & header/footer elements. To proper working you should wrap your table into parentView and add your menu to that parentView rather than to the UITableView. You can also try to call reloadData method on table since it can restore the content size of the view.

How to get UITableView header view to come before table cells in responder chain?

I have a rather large table header view above my table view. I have a number of subviews in that header view. I am doing something a bit nonstandard where I am "sticking" some of those subviews (but not all) at the top of the table view.
My problem is that although visually the table view cells pass under the sticky table header subviews, it seems that the table view cells are "above" the table header view in terms of touch response. (For example: I have a button that is a subview of the table header view. When there are no cells underneath the button, the button works great and responds to touches. However, when the user scrolls the table view so that there are cells underneath the button, a touch on the button actually selects a hidden cell rather than push the button.) Can anyone give advice on how to "raise" the table header view above the table view cells, so that its subviews get first shot at touch handling?
I am using a table header view rather than a section header view due to the fact that I only want some of the subviews to stick (letting the others scroll up off the screen as usual). One of the subviews also can be expanded (and that expansion is animated) - to a height that is even bigger than the entire height of the screen. For these reasons, I didn't think using a section header view would be feasible. (If it is, please let me know, as I know that section header views are "above" table cells when it comes to touch response.)
You may try this, which would keep the desired header view on top of the others.
[yourView bringSubviewToFront:yourSubView];
This may be able to help as well:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/5222-keeping-subview-front.html
Is this what you were looking for or did you want another approach?

Resources