What does clearsSelectionOnViewWillAppear do? - ios

clearsSelectionOnViewWillAppear is a boolean property of table and collection views, and it's set to true by default. According to the Apple Reference:
A Boolean value indicating if the controller clears the selection when the table appears.
What does that mean?

Let's say that, as is usual, you tap a cell and this presents a new view controller. Okay, but how does it do that? Your tap selects the cell. That is a visible change in the cell - the cell is highlighted.
Later, you dismiss that view controller and return to your table / collection view.
The question is: should the selection (caused by your earlier tap) still be showing as a selection? Should the cell still be visibly highlighted? The default is: no. We clear the selection before you visibly return to the table / collection view.
(You could do this yourself in your viewWillAppear: implementation, but the table / collection view controller is willing to do it for you.)

Related

(iOS)How to delete row when viewcontroller poped?

I have a UITableView. When I click on a row, it will push a detail view controller to show detail info.
And, if user clicks the "Done" button on the detail view controller, I need to pop this controller and delete row on tableview controller.
I try write code on detail view's viewWillDisappear:
if let vc = navigationController?.viewControllers[0] as? TodoViewController{
vc.removeTodo(at:todoListIndex)
}
The code work fine, but just a warning:
UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes.
So, What is the correct way to delete row?
As the error message suggests, it is there to remind you to avoid updating the view when it isn't visible. This typically happens when you update a view in a viewWill____ state (in this case viewWillDisappear).
So what you want to do is delete the cell from TableView in viewDid___ (such as viewDidAppear)lifecycle. Then, you will no longer see warning messages.
If needed, you can use help from flag members and observables to guide correct deletion timing.

iOS Storyboard - TableView Embedded in Container with Dynamic Height

I have a tableview that is embedded in a container, its using prototype cells so in theory I don't know how many cells there may be (realistically 1 - 3). At the minute, I end up with a bunch of empty rows underneath the table cell that is populated. Changing the height of the container can sort of 'hide' these but it isn't the solution.
How can I get the containers height to change depending on the number of items in the embedded tableview? Or alternatively if anyone knows if there is some magic "don't add any empty rows if there are only 1 or 2 items in my table view" setting?
You will have to set the frame of the Container with the embedded TableView based on the contentSize of the TableView. Note: depending on how you setup the TableView data sourse, the contentSize property of the TableView might not be set in viewWillAppear. Instead, you will have to use the viewDidAppear method of the ViewController that has all of your Container views. Also, you might need to call reloadData on your TableView after you set the frame of the Contatiner view.
Another option is to use the -prepareForSegue: method to make your parent view controller a delegate of the table view controller. When cells are added/removed from the table view, it can call a method on the parent view controller and pass the info; the parent view controller can then change the size of the container based on that information.
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

didSelectRowAtIndexPath in UITableViewController not working

I have a HomeViewController which is loaded with views. On the top is the map view, so now when I create the table view on the top of it which does not cover the whole screen but little portion of it(actually its a drop down list in the form of tableview), it displays fine with cellForRowAtIndexPath working fine but didSelectRowAtIndexPath is not working at all.. I have tried using break point on it but its not calling this method actually infect it is selecting the view at the back i.e. the map view. Clicking on the table view is actually changing the map view behind..
I am having the table view coming from another class which is inherited by the HomeViewController
I am using some NIDropDown class .. Its working fine if no view is behind it (only superview) but not woking if it has any view behind
Few things to be ensured-
1) As you said your table view is on top of the map view. If this is the case than it's alright.
2) Check if the user interaction is enabled for the table view or not.
3) Check if you have set the delegate of the table view(responsible for detecting events on table view).
4) Check if you have not set the delegate to nil somewhere and still the table view is visible.
I think if you follow the above steps than table view's events should be triggered, otherwise it will be good if you post your code to get some other clue regarding the issue.
That cellForRowAtIndexPath works is an indication that the datasource property (UITableViewDataSource) is properly set.
If didSelectRowAtIndexPath is not firing, you should check if the delegate property (UITableViewDelegate) is properly set, either in Interface Builder or in code.
After read your problem, i strongly believe there will be two issue :
1) Map view issue, if you shows table view as *callout view
Reason:
You supposed to use “MKAnnotationView” callout view for show drop down over map.
if you do so then make sure MKAnnotationView view height would be exact same of your drop down view’s(NIDropdown view’s) height.
Table view not able to get touch, that's why it would not send message of didSelectRowAtIndexPath to it's deletegte.
In your case might be Map view getting touch, which did not suppose to get until table view is at screen.
2) Table view rest out of bound on super view
Reason:
in such case table view display normally but actually its bound goes out of own super view bound.

iOS picker view as default input method

I have labelA in my custom tableview cell. When I click on the label I want picker view to be shown in the bottom of the screen in order to choose available options.
My question is: Do I actually need drag and drop PickerView into my view in storyboard or I can just invoke it as default input method? (Same as keyboard for text field)
If yes, how do I do that? - All I want when label is clicked show picker and hide it on when selection is finished. Could you please provide me some examples?
I am developing under swift but iOS example is fine as well.
Thanks for any help!
Edit:
Well label is not principle, I can use just input trigger from the cell directly. Idea after click show picker view and hide it after selection.
What I mostly do is use a UITextField and set the inputAccessoryView property to an instance of UIPickerView. This will display the UIPickerView once you tap on the UITextField instead of the default keyboard.
You can instantiate the UIPickerView all in code in your tableView:didSelectRowAtIndexPath method. You can either add it to your view controller using self.view addSubView. You will have to write code to place it properly on the screen with your table view. You might want to resize your table view so it shrinks and your picker view is shown on the bottom of your view.
An easier way could be to show a modal dialog with your UIPickerView already set up.
UPDATE
Create an new controller that is of type UIViewController.
Drag a UIViewController onto your storyboard and give it the type of the above controller and a storyboard id. I use the name of the UIViewController created.
Add your picker view to that and configure your data source.
Create a delegate for the option selected and have it return the item selected.
- (void)selectionMade:(NSString *)selectedValue;
In your table view controller, implement that delegate method above.
In tableView:didSelectRowAtIndexPath, load that view controller and show it.
[self.storyboard instantiateViewControllerWithIdentifier:#""]
Your delegate method will get that value and you can retrieve the cell using tableView.indexPathForSelectedRow.
Update you cell accordingly.

iOS- Table row/cell behaviour when it has a details disclosure button?

In my tableview I have a set number of rows and all have a detail disclosure button, clicking on detail disclosure button takes the user to a new view displaying info in a text view. I am not able to conclude on how the row should behave when user taps it because the Apple guidelines for table view and for detail disclosure button is confusing.
Please can anyone explain the below 2 scenarios for me. Thanks in advance.
Is it correct that the new view slides in only when the user taps the detail disclosure button and nothing happens when user taps elsewhere in the row.
What should be the behaviour if the user taps on the detail disclosure button with regard to highlighting?
What should be the behaviour of the table row if user taps elsewhere in the row ?
Apple guidelines says "When a detail disclosure button appears in a table row, tapping elsewhere in the row does not activate the detail disclosure button; instead, it selects the row item or results in app-defined behavior."
and Apple table view guidelines says "A table row highlights briefly when the user taps a selectable item. If a row selection results in navigation to a new screen, the selected row highlights briefly as the new screen slides into place. When the user navigates back to the previous screen, the originally selected row again highlights briefly to remind the user of their earlier selection (it does not remain highlighted)."
When you tap on the row this delegate method is invoked:
– tableView:didSelectRowAtIndexPath:
When you tap on detail disclosure button the following delegate method is invoked:
– tableView:accessoryButtonTappedForRowWithIndexPath:
tableView:accessoryButtonTappedForRowWithIndexPath:
Tells the delegate that the user tapped the accessory (disclosure)
view associated with a given row.
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
Parameters
tableView
The table-view object informing the delegate of this event. indexPath
An index path locating the row in tableView.
Discussion
The delegate usually responds to the tap on the disclosure button (the
accessory view) by displaying a new view related to the selected row.
This method is not called when an accessory view is set for the row at
indexPath. Availability
Available in iOS 2.0 and later.
Declared In UITableView.h
tableView:didSelectRowAtIndexPath:
Tells the delegate that the specified row is now selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath Parameters
tableView
A table-view object informing the delegate about the new row selection. indexPath
An index path locating the new selected row in tableView.
Discussion
The delegate handles selections in this method. One of the things it
can do is exclusively assign the check-mark image
(UITableViewCellAccessoryCheckmark) to one row in a section
(radio-list style). This method isn’t called when the editing property
of the table is set to YES (that is, the table view is in editing
mode). See "“Managing Selections”" in Table View Programming Guide
for iOS for further information (and code examples) related to this
method. Availability
Available in iOS 2.0 and later.
See Also
– tableView:willSelectRowAtIndexPath:
– tableView:didDeselectRowAtIndexPath:
Declared In UITableView.h
Reference : UITableViewDelegate
You can do whatever you want with this discloure button. 1)New views can slide event if will tap on row. 2) nothing 3) you can dot whatever you want: show new UIViewController, UIAlertView etc. It depends on your imagination. There are no restrictions for this.

Resources