I have a UISearchBar implemented into my UITableViewController, my problem is when I scroll down in my table view, the search bar acts like a cell and scrolls with the table, therefore disappearing. Is there a war to programmatically fix this?
I know there are similar questions on SO with this issue and I have exhausted all the resources provided in those posts.
Thanks
Sure, don't use an UITableViewController but use a normal UIViewController with UITableView and UISearchBar inside.
When you use an UITableViewController, its UIView is directly the UITableView. So every control and subview go in the tableView. For this reason you need to change strategy and use the way above.
Related
How do you add a search bar to a collection view controller in iOS 12? I realize this should be a simple question but I'm having difficulty finding an example that doesn't look half-baked.
This can either be just using a search bar or a search controller. I don't know what the best approach should be in iOS 12. I would also like to do this programmatically.
Use a UISearchController. Put the collection view controller into a navigation controller and set the searchController of its navigationItem. Now the search bar will appear in the navigation bar. You can configure things so that the search bar appears only when the user scrolls the collection view all the way down; that is effectively the same as making the search bar be at the top of the collection view. This is a nice interface and is what Apple expects you to use.
I am a bit confused, it is a very strange thing to put the UISearchBar in the UICollectionView. Because the UICollectionView the display uses the UICollectionViewCell, each UICollectionViewCell can be reused.
So I guess you are not going to put in the UICollectionView, but search the results through the SearchBar, using the UICollectionView to display the results.
Sorry, my network can't upload images. The layout is probably UISearchBar and UICollectionView in UIViewController.
- UIViewController
- UISearchBar
- UICollectionView
In the UISearchBarDelegate proxy method, the network request can be executed when the search is triggered, and the data returned by the network request is refreshed on the list.
If you are not very demanding on the layout of the list, you can use UITableView, UICollectionView needs to set UICollectionViewLayout to be slightly more complicated.
In code huh? Using storyboards all you need to do is add the search bar and connect it to an IBAction outlet. When it changes you pass the value to a predicate and reload your collection. Delegate and other Mac Search bar options can be set in code or right in the storyboard. Sorry I couldn't fully bake it for you!
I've a UIViewController which contain an UITableView with a UIRefreshControl as a subview of this tableview.
When there is at least one element in the tableview, everything is fine. I can pull-to-refresh without issue.
However, I created recently a new custom view which mean to be displayed above the tableview when there is no element in it. It look like this:
The issue is: when there is this "no element" custom view displayed, my UIRefreshControl does not work anymore. It's a normal behavior: there is a view above my tableview so gesture controls are not working on the tableview.
So my question is: how can I manage my view so I will be able to pull-to-refresh even if there are this "no element" custom view above my tableview?
Thank you!
First you disable the user interaction of customView. Then may be it will be help you to resolve your problem.
Ok so I did not thougth enought about this question...
I just simply need to add this customview as a subview of my tableview.
Generally:
Is it OK to add a UITableView as a subview of another UITableView? Or, should I create a UIView and add each UITableView to it?
Specifically:
For a ComposeTableViewController with a typeahead, like in the iPhone's native Mail app, which approach would you recommend and why?
Note: I prefer to construct things 100% programmatically (no Interface Builder).
Subclass UITableViewController.
Then, to show the typeahead results, create and add a resultsTableView as a subview of self.tableView positioned directly underneath the the cell (of self.tableView) with the typeahead text field.
The nice thing about this approach is that resultsTableView scrolls with self.tableView automatically.
But, is it OK to add a UITableView as a subview of another UITableView?
Subclass UIViewController.
Create and add tableView (custom property) as a subview of self.view.
Create and add resultsTableView also as a subview of self.view.
The annoying thing about this approach is that I have to reposition resultsTableView manually anytime self.tableView scrolls.
I think I'd prefer approach 1, but adding a UITableView as a subview of another UITableView just seems smelly to me.
TableViews cannot have subviews. You can try adding a tableview as the view of a TableViewCell, but then you have to ask yourself how it would scroll, if you tried scrolling in the subtableview would it scroll the child tableview or the parent tableview? It is much easier to present multiple tableviews within a view. This can be done easily by setting your custom viewcontroller as the datasource of both tableviews contained within its view and then comparing the tableview pointer that is sent as a parameter of the datasource method to the two tableview pointers that are IVars of your custom view controller.
Hold stuff like this in a container. Create a container view controller which only does typeahead. This container view controller will hold your main table view controller (and its table view) and it will hold the typeahead view controller (and its view).
It will keep the logic needed for the typeahead out of your main table view and as a bonus you might end up with a reusable typeahead container which could be applied to other views.
Yes it is good to go for adding UITableView in as a cell of another UITableView.
I had one issue and posted a question
Multiple Views as subviews
And requirement was like a group of controls with list of other controls. I thought that time that it will be messy if i'm going to add UITableView as a cell of UITableView.
What i found that... Boy !! it's how iOS meant to be. There is no any lag while doing this.
Tested for below functionalities:
Scrolls like a charm
Separate delegates called for each
Reordering of both UITableView cell
Swipe and remove cell
Runtime datasource update for both UITableView and reload both at the same time.
There is no any reason not to add subview UITableView.
You can do it but it is against the principle of MVC. You will be setting the delegate of a view to another view which is wrong.
I'v never used UITableViewControllers or UICollectionViewControllers, because you can have the same functionality by using UIViewController with its root UIView, then adding UITableView in xib or storyboard and assigning its delegate and datasource. And I also was able to put activity indicator inside the center of root UIView.
Now the things get a little bit complicated when using UICollectionViewController where its root view is UICollectionView. I need to update some code of other guys and they put activity indicator inside UICollectionView (in storyboard). The problem is this activity indicator gets hidden when cells are reused because activity indicator view is the most bottom one. I was unable to change visibility priority storyboard and also this code in view didLoad is not working:
[self.itemsCollectionView bringSubviewToFront:self.activityIndicator];
Because labels, images and etc. views of collection view cell are placed later, during collectionView:cellForItemAtIndexPath:. I could try to call bringSubviewToFront: in collectionView:cellForItemAtIndexPath: but that would be a wrong decision. Any ideas how to achieve this when using UITableViewControllers or UICollectionViewControllers?
IMHO the only reason to use UITableViewControllers or UICollectionViewControllers is because static cells are not shown storyboards when designing layout.
UPDATE It appears iOS wraps UICollectionView inside UICollectionViewControllerWrapperView. Tried to add activity to this wrapper view in viewWillAppear:
[self.itemsCollectionView.superview addSubview:self.activityIndicator];
But with no luck - activity indicator is still is below the cells.
I'v ended in refactoring existing UICollectionViewController in storyboards: I'v opened storyboard xml file with TextEdit, searched for the screen and changed its type from collectionViewController to viewController because was unable to find another way how to change the type of controller, though I hope there will appear some more elegant way for that in the nearest future. Then, I'v wrapped collectionView inside root view and placed activityIndicator inside this view. It's proven classical approach that works like a charm.
I've got a UITableViewController that inserts custom UITableViewCells into a UITableView. Is there a way that I can put this UITableController/View combo into different views? I've got the behavior of the UITableView acting the way I want, I just want to use that UITableView in different UIViews (say a UIView with a UILabel above the UITableView and one with a UILabel below the UITableView).
I know I can make a UIViewController that has an embedded UITableView in it and have the UIViewController act as the UITableView's delegate, but then I would have code reuse (the UITableViewController logic would be in multiple UIViewControllers). Or am I looking at this problem the wrong way?
I want to somehow reuse the functionality of my UITableView in different UIViews.
Thanks
Yes, you can, simply instantiate (or get a reference to) the UITableViewController inside the UIViewController and call something like this:
[self.view addSubview:tableViewController.tableView];