Keyboard hiding tableview cells containing textfields - swift

I have a uiviewcontroller that contains a uitableview, this uitableview is full of custom cells that contain a textfield.
This view in storyboard is loaded as a popover, so the behaviour is:
iPad - If the keyboard is loaded and the keyboard hides the current editing cell, the cell automatically moves up so it is visible, regardless of its position in the tableview
iPhone - It doesn't, the tableview dosn't move if the keyboard hides the cell.
Can anyone suggest an elegant way round this?
Thanks

I had similar issue using Xcode 8.2.1, swift 3 with keyboard hiding UITextView when it becomeFirstResponder(), spent few hours to figure out what was the problem without any luck, finally created new test project with single UITableViewController and it worked, thereafter re-created the Table View Controller in storyboard - cells started to scroll to allow space for keyboard which appears just below the designated text view as expected...
perhaps something with wiring that prevented it from working properly

You can use the table view method scrollToRowAtIndexPath
func scrollToRowAtIndexPath(_ indexPath: NSIndexPath,
atScrollPosition scrollPosition: UITableViewScrollPosition,
animated animated: Bool)
to scroll the table view when a text field becomes the first responder, for example:
let indexPath = NSIndexPath(forItem: 4, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
See the UITableView reference for further information
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/doc/uid/TP40006943-CH3-SW44
I hope this helps you. Feel free to ask if anything is unclear.

Related

Horizontal NSCollectionView in vertical NSTableView does not select until collection view is clicked

I have a weird question regarding NSCollectionView. Basically, I have a collection view that scrolls horizontally in a vertically scrolling table view. I have implemented the data source and delegate of the collection view within my NSTableCellView subclass. The data source works just fine and the collection view is able to load some images.
Here comes the problem. I want the collection view to be selectable. I have implemented this delegate method into my table cell view subclass:
func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
// Do things here
}
This method works, but with a caveat. It only gets called when I click on an empty space in the collection view first and then selecting a cell. If I only click on a cell, it will not get called, which is quite bizarre to me. Any idea on how to fix this?
I designed the table cell view in the storyboard, the collection view has it's Selectable and Allow Empty Selection checked in the storyboard. Allow Multiple Selection is not checked because I don't want multiple selection, but I tried having it on and it doesn't change anything.
Thanks
Subclass NSTableView. In your subclass, override validateProposedFirstResponder(_:for:) to return true for your collection view and its subviews (or maybe just return true always).

UITableViewCell StackView Hidden Elements

I have a TableViewCell that has 2 StackViews. One of the StackViews has 3 components and one of which is hidden. By pressing on a button, I want the hidden UIImageView to appear. While my code does this, it does not format it correctly as the height of the TableViewCell does not change, as I would like it to. I have tried calls to sizeToFit(), but I am starting to realize that this will not affect the height of a cell. When the cell leaves the view and comes back, it draws correctly. How can I update the height of the cell when the button is pressed?
After writing out this question, I started to realize that the problem was from the cell's height. After doing a little bit more research I found out that this can be achieved with the
tableView.beginUpdates()
tableView.endUpdates()
I had to create a delegate and add a delegate method to reference back to the ViewController.
Hope this helps someone else!

Segmented controller

UIView,SegmentedController & UIContainerView in a ScrollView or something similar?
In my storyboard I have a VC containing a UIView at the top, segmented controller in the middle & 2 ContainerViewControllers at the bottom with embeded segue's to new VC's.
In the new VC's I have a TableView & a CollectionView (For example, the Instagram profile).
My issue is that I need the SegmentedController and UIView to scroll with the ContainerView(TableView/CollectionView), at the moment only the ContainerView part scrolls and the parts above that are fixed.
Now I'm guessing I probably need to put everything into a UIScrollView, so I tried that & placed all the costraints correctly. But when It came to configuring it in the Swift file, I only really now how to set the height of the scroll but oviously the height I need can vary and isn't a fixed height!
If anyone can help me here that would be fantastic, or maybe point me towards a similar question already asked here? I have already looked, but did not find anything unfortunately!
Here's an image below explaining basically what I'm after, if above I wasn't very clear..
here is an example you can see: https://github.com/Jackksun/ScrollIssue
the image is here, as I dont have enough reputation I cannot place it in the post!
You can use only UITableViewController. You will define three custom cells and in cellForRowAtIndexPath ask for the Index number, if it is 0 you will set the elements in that cell accordingly
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: TableViewCell!
// stuff in UIView
if indexPath.row == 0 { set the cell here}
// for UISegmentedController
if indexPath.row == 1 { set the cell here}
// cells that will repeat
if indexPath.row >= 2 { set the cell here}
return cell
}
Update1
Define how many custom cells you will use in Attribute inspector for tableView, you will see them instantly in Storyboard
Update 3
Did you mean this? It is completely table view with 4 custom cells. I didnt use Collection view at all but I think it works as you wanted

(iPhone/iOS) UISearchBar Scope Buttons cover first row of UITableView

I know this question has been asked before, but I have yet to find a true solution. I have a UITableView with a UISearchBar as its tableHeaderView. Whenever the scope buttons appear, they cover the first row of the UITableView.
I have tried using UITableView.contentOffset to offset the UITableView by the height of the scope buttons, and this works until the user taps the UITableView or scrolls it, at which point the UITableView jumps back to its original offset.
The best I've been able to do is set the UISearchBar as a separate item in the view, and simply resize the UITableView dynamically to show or hide it when the user taps a search button. The only problem is that there is no animation, so the transition is sharp.
UPDATE: I solved the problem by implementing a UISearchDisplayController, as this was the cleanest and easiest solution to work with. The UISearchDisplayController automatically shows/hides the scope buttons and moves the UITableView down accordingly. I didn't do this at first because I wanted the UITableView to remain visible rather than initially being blacked out by the UISearchDisplayController, however I've come to realize that the blackout does not hinder any functionality; the user would not have initiated a search if he wanted to continue looking at the UITableView the way it was.
Ideas:
Set the tableHeaderView to a UIView that has the UISearchBar as its subview, and resize that UIView when the scope buttons appear. The UITableView will hopefully respond to that view changing its frame.
Set the UISearchBar as the content of the first cell of the UITableView and use (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation when the scope buttons hide or show. Return the proper height to have the cell animate to its new height.
Or you could use the method you describe (UISearchBar as a separate item in the view), and use an animation block to animate the transition so it's less sharp.
[UIView animateWithDuration:1.0 animations:^
{
//change table view frame here
}];
If you adjust the cell height of the header based on if the search controller is active or not, all works well
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
var headerHeight: CGFloat = 0.0
if searchController.isActive
{
headerHeight = searchController.searchBar.frame.size.height
}
return headerHeight
}

How to interact with a UITableView displayed overlapping another UITableView

I have one UITableView displaying some data, and when the user begins typing in a UITextField above the UITableView, another UITableView is displayed below the UITextField to show suggestions. This "suggestions" UITableView overlaps the UITableView below the UITextField. Tapping on one of the suggestions will populate the text box with the item tapped.
The suggestions UITableView displays in front of the other UITableView, but problem is that none of the taps to the suggestions UITableView register if they're outside the bounds of the view that contains it.
This is probably a really simple problem dealing more with UIViews in general, but I'm having trouble. Thanks for the help!
You have to adjust your second tableview which is giving you suggestions below testfield which will solve most of your issues
hope it helps you
you can show the second UITableView within another view that is displayed above the current view, like a popover, using
[self.view addSubview:secondView].
Once a selection is made, you can remove this view using
[secondView removeFromSuperview];