Hide TableView when cancel button clicked - swift

i’m trying to realize a search bar with a TableView. Everything is working like this: when i touch the search bar for add text the TabeView(before hidden) compare with the auto compilator and there i write. What i’m trying to do is hide the TableView when i tap the button cancel and hide again the tableView but is not working correct because after that i tap cancel and my table hide as i want then i can’t TableView is not working. I tried using "tableview is hidden when cancel button clicked" but my table doesn't work later. How can i do? Do you have some ideas? Thank you guys
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
TableVieww.reloadData()
TableVieww.isHidden = true
}

Try:
TableVieww.layer.alpha = 0.0

Related

make NSTableView row stay selected Swift

how would I go about making a previously selectedRow of an NSTableView stay selected after the user presses a button calling a method to work with that selectedRow?
It worked in a previous programm looking like this:
selecting it gives it the OSX selection color, clicking the save button makes the textField becomeFirstResponder() which makes the row go grey automatically and doesnt change the selectedRow property. This is not done by my code, so im happy it just works.
Now in a similar program I want to achieve exactly this, but after clicking a button (calling a method making the textField becomeFirstResponder()) the visual selection is gone and also the tableView property selectedRow goes back to -1 (no row selected).
I tried a lot of ideas, most answers online are objective-C. Here is my most recent code (beeing called by the same button and pretty much faking what worked automatically before):
the NSTableViewAction method and the selectedRowCache variable:
var selectedRowCache = -1
#IBAction func alarmDataTableView(_ sender: NSTableView) {
selectedRowCache = alarmDataTableView.selectedRow
}
the buttonAction:
#IBAction func editAlarmButton(_ sender: NSButton) {
let row = alarmDataTableView.rowView(atRow: self.selectedRowCache, makeIfNecessary: false)
row?.backgroundColor = .blue
}
the selectedRowCache is just a variable that saves the selectedRow whenever the user selects a row (i dont want this).
no with this code the background color of the row doesnt do a thing (besides losing its selection after the button method is called).
the NSTableView is view based.
Thank you for any help!

Swift Trigger click in textfield

In my app I'm using some pickerviews that appear when I click on a textfield, that works fine!
I wanted to do the same when I click on a Left Bar Button Item. I can't do it with the button because buttons doesn't have inputView property, needed for associate the pickerview to the button (in this case). So I want to have a hidden textfield that is programmatically clicked when I click on the button (when it's clicked it show the pickerview and change the button name, that's all done)
Is that possible?
The best I can do right know is something like this
txtFantasma.perform(
#selector(becomeFirstResponder),
with: nil,
afterDelay: 0.1
)
It works fine, but just work at the first time.
EDIT1:
I've tried to make it with buttons... The popover is showing, now I wanted to click in one button and dismiss the popover and pass data to the main viewcontroller.
class ViewPopup:UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btTituloAsc(_ sender: UIButton) {
let next = self.storyboard?.instantiateViewController(withIdentifier: "mainview") as! ViewController
next.ordenacao = "TituloAsc"
self.present(next, animated: true,completion:nil)
}
}
This works, but the main controller is showed without the Navigation Bar! How can I do the same but show de Navigation Bar?
The simplistic way to do it, is just display the pickerView as a popover once the user taps the button and then change the buttons title accordingly.

OSX NSTextView becomes uneditable after showing NSAlert,NSOpenPanel

I've got a NSTextView in my view that works as it should, but when I show a NSAlert and close it, it becomes uneditable (with text still selectable). The NSAlert is a save/cancel alert that updates the textView's string when the user selects save, the string is not updated when the user presses cancel. In both cases the textView was uneditable, the alert is shown when the users has made changes and wants to change the tableView selection.
It feels like the textView refuses first responder but when breaking and checking in the console its "true", I also checked some other values after the view was uneditable:
isEditable was True
isSelectable was True
canBecomeKeyView was True
acceptsFirstResponder was True
acceptsTouchEvents was False, tested True but did not work
My "test" setup:
Video, same when triggering the popup from a tableview selection change and a button : video
My popup code
func dialogOKCancel(question: String, text: String) -> Bool {
let myPopup: NSAlert = NSAlert()
myPopup.messageText = question
myPopup.informativeText = text
myPopup.alertStyle = NSAlertStyle.warning
myPopup.addButton(withTitle: "OK")
myPopup.addButton(withTitle: "Cancel")
return myPopup.runModal() == NSAlertFirstButtonReturn
}
let answer = self.dialogOKCancel(question: "Ok?", text: "Choose your answer.")
also tried:
let a = NSAlert()
a.messageText = "Delete the document?"
a.informativeText = "Are you sure you would like to delete the document?"
a.addButton(withTitle: "Delete")
a.addButton(withTitle: "Cancel")
a.alertStyle = NSAlertStyle.critical
a.beginSheetModal(for: self.view.window!, completionHandler: { (modalResponse) -> Void in
if modalResponse == NSAlertFirstButtonReturn {
print("Document deleted")
}
})
Stuff I tried:
Remove any textView updates, showing the alert still breaks it
Dragging a new "untouched" textView in my storyboard but now both textViews became uneditable
I tried showing the NSAlert when clicking a button instead of when changing the tableView's selection. Here the textView I was editing stays first responder but as soon as Ieave the textView, its uneditable again.
I tried triggering just an animation instead of the NSAlert, here the textView keeps working
I Tried replacing the NSAlert with a overlay View that had a title/description/buttons. when showing this dialog, the textView also became uneditable
I'm stuck on this for a long time and any help is greatly appreciated,
Thanks
After long time of debugging, I found this line to be the one that broke the textfields, I will leave this post online in case someone else stumbles upon this weird problem
window?.styleMask = NSFullSizeContentViewWindowMask
removing this line fixed the problem.

Changing collection view's isHidden property doesn't work with Search Bar cancel button

I have a collection view that is hidden on ViewDidLoad. When a user taps on the search bar, the collection view is shown. But when I click on the cancel button in the search bar, the collection view is not hidden. I read on other posts to use .alpha = 0 to hide a view and that works. My questions are:
Why isHidden not working and .alpha is?
Is collection view still the active layer if I use .alpha = 0?
Here is the code:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
collectionView.isHidden = true // This doesn't work
//collectionView.alpha = 0 // This works
}
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
collectionView.isHidden = false // This doesn't work
//collectionView.alpha = 1 // This works
return true
}
Animated GIF showing app working by changing alpha channel to 1 and 0
Thanks for reading my question.
This hidden property works best on UIViews (while used for many other things). It's best to embed your collectionView inside a view if you want to use the hidden property.
The default value of this property is false. As an optimization, the
collection view might not create the corresponding view when the value
of this property is true. Because there might not be a view, hidden
elements do not participate in hit testing for the collection view.
Source: https://developer.apple.com/documentation/appkit/nscollectionviewlayoutattributes/1535336-ishidden

setEditing method delete button doesn't work

In my UIViewController, I have an UITableView which contains different cells with saved contents by using the NSUserDefaults. I also have a custom UIBarButtonItem which causes the UITableView to enter and leave editing mode.
This all works, but the problem is when I tap the round red button and on the right side of the cell appears the delete button, the delete button doesn't work... I can tap on it as much as I want but the cell does not get deleted.
#IBAction func EditListButton(sender: UIBarButtonItem) {
if ShoppingListTable.editing {
ShoppingListTable.setEditing(false, animated: true)
self.EditListButton.title = "Edit"
} else {
ShoppingListTable.setEditing(true, animated: true)
self.EditListButton.title = "Done"
}
}
Use UITableViewDataSource's tableView:commitEditingStyle:forRowAtIndexPath: to detect a tap on the delete button.
Then in the implementation delete the row from your data source (NSUserDefaults in your case).
So in your tableView's dataSource (ie. in the class you implemented numberOfSectionsInTableView: for example), add your custom implementation of tableView:commitEditingStyle:forRowAtIndexPath: where you delete the underlying data.