edit a TextView in a TableView cell in swift - swift

I'm stuck: I have a TableView populated by .xib cells that I made. Each of these cells contains an editable TextView.
I'm trying to save on my Firebase database the text that the user input in those TextViews. I don't want to implement any button, the text should be saved as soon as the TextView editing end.
I tried to connect the TextView from the .xib file to the UITableViewCell class but it doesn't allow me to connect it as an IBAction but only as outlet or outlet connection.
Please Help me, thanks!
screenshot

You need to implement the UITextFieldDelegate in your
UITableViewCell class.
Connect the delegate of the UITextView to the cell class.
Do whatever you want in the func textViewDidEndEditing(UITextView) which you need to implement in your cell class.
Here you can read more: https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618603-textviewshouldendediting?changes=_2

I solved replacing the TextViews with TextFields that could look the same but could be linked to the UITableViewCell.swift as IBAction.
Thus I wrote the code to update the "comments" section of my database inside the IBAction:
#IBAction func commentTextFieldToggle(_ sender: UITextField) {
if commentTextField.text != "" {
let comment = commentTextField.text
// I declared the next 7 constants to retreive the exact position of the string "comment" that I want to change
let date = dateLabel.text!
let time = timeLabel.text!
let year = date.suffix(4)
let day = date.prefix(2)
let partialMonth = date.prefix(5)
let month = partialMonth.suffix(2)
//I use this "chosenDate" constant to retreive the database query that I previously saved using the date in the below format as index:
let chosenDate = "\(year)-\(month)-\(day) at: \(time)"
let commentsDB = Database.database().reference().child("BSL Checks")
commentsDB.child((Auth.auth().currentUser?.uid)!).child(String(chosenDate)).child("Comments").setValue(comment) {
(error, reference) in
if error != nil {
print(error!)
} else {
print("User Data saved successfully")
}
}
}
}

Related

Saving with NSDocument in a Second ViewController

I am trying to simply save using NSDocument to a rtf. The code works fine but when I try to save to a view controller that isn't the initial child to the window controller it throws an error from a modal saying 'The document “” could not be saved as “”.'
How can I save the file to the Second View Controller?
Window Controller
|
Login View Controller
| |
SidebarViewContoller ViewController1
|
TableViewController 2 Replaces VC1
Save TextView in this VC
I want to be able to write data into My NSDocument from the textView in ViewController2 and save it to the desktop
Just like you would for instance in Pages
Here is the code
// Document.swift
class Document: NSDocument {
var text = NSAttributedString()
var documentViewController: DocumentViewController? {
return windowControllers[0].contentViewController as? DocumentViewController
}
override init() {
super.init()
// Add your subclass-specific initialization here.
}
override class var autosavesInPlace: Bool {
return true
}
override func makeWindowControllers() {
// Returns the Storyboard that contains your Document window.
let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController
self.addWindowController(windowController)
}
override func data(ofType typeName: String) throws -> Data {
// Save the text view contents to disk
if let textView = documentViewController?.textView {
let rangeLength = textView.string.count
textView.breakUndoCoalescing()
let textRange = NSRange(location: 0, length: rangeLength)
if let contents = textView.rtf(from: textRange) {
return contents
}
}
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
override func read(from data: Data, ofType typeName: String) throws {
if let contents = NSAttributedString(rtf: data, documentAttributes: nil) {
text = contents
}
}
//SecondViewController
override func viewDidAppear() {
let document = self.view.window?.windowController?.document as! Document
textView.textStorage?.setAttributedString(document.text)
}
The way you have things set up in your app, you are not going to be able to put an entire NSDocument's contents in a text view. Suppose you have 10 items in the table view and selecting an item fills the text view with some text. You're not going to be able to have a NSDocument for each of the 10 table view items inside a single document.
What you are going to have to do is create a data structure that represents a text file. Think of a chapter in a book or a scene in a screenplay. In your NSDocument subclass, you will have an array of these data structures. When you save the document, you will save it as a file wrapper, which is a directory of files that looks like a single file in the Finder. There will be one text file in the file wrapper for each item in the table view. Refer to the following article to learn more about file wrappers:
Working with File Wrappers in Swift
Now what you want to know is how to fill the text view when the table view selection changes. This is painful to do with Mac storyboards because the table view's controller and the text view's controller are separate scenes. Implement the delegate method tableViewSelectionDidChange in the view controller for the table view. Use the parent property to get the split view controller. Use the split view controller to get the text view's controller. Pass the selected row to the text view's controller and use that to get the right text file to display in the text view.
The split view controller should store a reference to the NSDocument. Use the parent property to access the split view controller from the text view's controller. With access to the split view controller, you can access the document to fill the text view with the contents of the text file corresponding to the selected item in the table view.

How to detect dismissed TextView in TableView?

I have a tableView with dynamic cells with multiple TextViews. I am dismissing a keyboard with a "Cancel" and trying to determine which TextView is being dismissed to "undo" the changes made by user.
Based on this similar question: How to determine which textfield is active swift I have adapted one of the answers for the following extension:
extension UIView {
var textViewsInView: [UITextView] {
return subviews
.filter ({ !($0 is UITextView) })
.reduce (( subviews.compactMap { $0 as? UITextView }), { summ, current in
return summ + current.textViewsInView
})
}
var selectedTextView: UITextView? {
return textViewsInView.filter { $0.isFirstResponder }.first
}
}
This is working and I am presently testing in the following code:
#objc func cancelButtonAction() {
if let test = tableView.selectedTextView {
print("View Found")
}
tableView.beginUpdates()
tableView.endUpdates()
}
Doing a break at print("View Found") I can inspect "test". The following is the result.
This appears to only identify the view Test by a memory address. My question is how do I interpret this to identify the view that was being edited?
Update: There seems to be some issue in understanding. Assume I have a table with two cells and in each cell two textViews (dynamic cells). Assume the table loads by saying in the 4 textViews. "Hi John", "Hi Sam", "Bye John", "Bye Sam". Suppose the user starts editing a cell and changes one cell to read "Nachos". Then the user decides to cancel. I want to then replace with the value that was there before (from my model). I can find the textView but it now reads "Nachos". Therefore I do not know which textView to reload with the appropriate Hi and Bye.
Implement a placeholder for your textviews so that when their text is empty, it will have a default value. Therefore, when a user presses cancel while in focus of a textview, we can set the textview's text to its default value. See link to implement a textview placeholder.. Text View Placeholder Swift
I did solve this problem by adding the .tag property to the textView objects. I also dropped the extension approach and used textView delegate. The solution required me to first assign the tag and delegate = self for each textView in the tableView: cellForRowAt. The following shows one TextView of many. Notice the tag is setup so I may determine the section and the row it came from and the specific item.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
...
cell.directionsTextView.delegate = self
cell.directionsTextView.tag = indexPath.section*1000 + indexPath.row+1
return cell
}
Two global variables are defined in my tableView class:
var activeTextView = UITextView()
var activeTextViewPresentText = String()
The textViewDidBeginEditing captures the original state of the textView text before the user starts editing.
// Assign the newly active textview to store original value and original text
func textViewDidBeginEditing(_ textView: UITextView) {
print("textView.tag: \(textView.tag)")
self.activeTextView = textView
self.activeTextViewPresentText = textView.text
}
Lastly, if the user cancels the editing, the original text is reloaded.
#objc func cancelButtonAction() {
if activeTextView.text != nil {
activeTextView.text = activeTextViewPresentText
}
self.view.endEditing(true)
tableUpdate()
}

How do I display the data fetched from called view controller into a dynamic tableviewcell of the calling view controller while using unwind segue.?

I have dynamic tableview, wherein one of the cell (duration) when tapped opens another view controller which is a list of duration viz (30 min, 1 hour, 2 hours and so fort). One of the durations when selected should display the selected duration in the first view controller. I am able to pass the data back to first view controller using unwind segue but unable to display the passed value. DOn't know whats missing.
I am displaying the code below:
FIRST VIEW CONTROLLER (CALLING)
#IBAction func unwindWithSelectedDuration(segue:UIStoryboardSegue) {
var cell = tableView.dequeueReusableCellWithIdentifier("durationCell") as! durationTableViewCell
if let durationTableViewController = segue.sourceViewController as? DurationTableViewController,
selectedDuration = durationTableViewController.selectedDuration {
cell.meetingDurationCell.text = selectedDuration
duration = selectedDuration
}
SECOND VIEW CONTROLLER (CALLED)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SaveSelectedDuration" {
if let cell = sender as? UITableViewCell {
let indexPath = tableView.indexPathForCell(cell)
if let index = indexPath?.row {
selectedDuration = durationList[index]
}
}
}
}
tableView.dequeueReusableCellWithIdentifier should only be called within tableView:cellForRowAtIndexPath:. It has no use outside this context.
The easiest fix is to just reload the table once you have stored the selected duration:
#IBAction func unwindWithSelectedDuration(segue:UIStoryboardSegue) {
if let durationTableViewController = segue.sourceViewController as? DurationTableViewController {
selectedDuration = durationTableViewController.selectedDuration
tableView.reloadData()
}
}
Note that this assumes you only need one selectedDuration for your whole table, rather than one per row. If you need one per row, I assume you have them stored in an array somewhere, so it is that array that should be updated instead before the reloadData.

NSTableView detect NSTableColumn for selected cell at start of cell edition

I'm trying to programatically get get a a column.identifier for the cell that is being edited. I'm trying to get by registering my NSViewController for NSControlTextDidBeginEditingNotification and when I get the notification I track the data by mouse location:
var selectedRow = -1
var selectedColumn: NSTableColumn?
func editingStarted(notification: NSNotification) {
selectedRow = participantTable.rowAtPoint(participantTable.convertPoint(NSEvent.mouseLocation(), fromView: nil))
let columnIndex = participantTable.columnAtPoint(participantTable.convertPoint(NSEvent.mouseLocation(), fromView: nil))
selectedColumn = participantTable.tableColumns[columnIndex]
}
The problem I have is that the mouse location is giving me the wrong data, is there a way to get the mouse location based on the location of the table, or could there be a better way to get this information?
PS. My NSViewController is NSTableViewDelegate and NSTableViewDataSource, my NSTableView is View Based and connects to an ArrayController which updates correctly, and I could go to my Model object and detect changes in the willSet or didSet properties, but I need to detect when a change is being made by the user and this is why I need to detect the change before it happens on the NSTableView.
This question is 1 year old but I got the same issue today and fixed it. People helped me a lot here so I will contribute myself if someone found this thread.
Here is the solution :
1/ Add the NSTextFieldDelegate to your ViewController :
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource, NSTextFieldDelegate {
2/ When a user wants to edit a cell, he had first to select the row. So we will detect that with this delegate function :
func tableViewSelectionDidChange(_ notification: Notification) {
let selectedRow = self.tableView.selectedRow
// If the user selected a row. (When no row is selected, the index is -1)
if (selectedRow > -1) {
let myCell = self.tableView.view(atColumn: self.tableView.column(withIdentifier: "myColumnIdentifier"), row: selectedRow, makeIfNecessary: true) as! NSTableCellView
// Get the textField to detect and add it the delegate
let textField = myCell.textField
textField?.delegate = self
}
}
3/ When the user will edit the cell, we can get the event (and the data) with 3 different functions. Pick the ones you need :
override func controlTextDidBeginEditing(_ obj: Notification) {
// Get the data when the user begin to write
}
override func controlTextDidEndEditing(_ obj: Notification) {
// Get the data when the user stopped to write
}
override func controlTextDidChange(_ obj: Notification) {
// Get the data every time the user writes a character
}

Reloading single row in PFTableViewCell - Returns that PFTableViewCell does not have cellForRowMethod

Ok so I've been trying to make a like button for an app I'm working on and I'm using parse as a backend. So far I can update the like button in parse, however, I can only reload the entire tableview not just the single cell. Reloading the entire tableview sometimes causes the tableview to move up or down I believe because of different sized cells.
#IBAction func topButton(sender: UIButton) {
let uuid = UIDevice.currentDevice().identifierForVendor.UUIDString
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = objectAtIndexPath(hitIndex)
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: hitIndex!) as TableViewCell
//var indexOfCell:NSIndexPath
//indexOfCell = hitIndex!
if object.valueForKey("likedBy") != nil {
var UUIDarray = object.valueForKey("likedBy")? as NSArray
var uuidArray:[String] = UUIDarray as [String]
if !isStringPresentInArray(uuidArray, str: uuid) {
object.addObject(uuid, forKey: "likedBy")
object.incrementKey("count")
object.saveInBackground()
//self.tableView.reloadData()
}
}
}
I've already tried the cellForRow with the proper indexPath and it always returns that PFTableViewCell does not contain the cellForRowAtIndexPath method. I read on a forum that someone was able to create a custom method to reload the cell and Parse's documentation uses PAPCache to do the like button. Lucky for me the documentation for the like button on Parse's page is in Obj-C and I've coded my app in Swift. Here's the link to the page if you want to see: https://www.parse.com/tutorials/anypic#like. Section 6.1.
TD;LR I'm unsure how to update the cell so the entire tableview does not get reloaded without the cellForRowAtIndexPath method. Maybe Custom method? Maybe PAPCache method?
SOLVED
if object.valueForKey("likedBy") != nil {
var UUIDarray = object.valueForKey("likedBy")? as NSArray
var uuidArray:[String] = UUIDarray as [String]
if !isStringPresentInArray(uuidArray, str: uuid) {
object.addObject(uuid, forKey: "likedBy")
object.incrementKey("count")
object.saveInBackground()
let count = object.valueForKey("count") as Int?
//cell.count.text = "\(count)"
self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.None)
This properly reloads only the cell, however, now my entire tableview always scrolls to the top which is not ideal.
I don't understand what are you trying to with the UUID; hitpoint ;hitIndex, and I don't understand what you want your app to do and I don't have and experience with parse ? I would comment on your question,but I need 50 rep to do this.Sorry that this isn't an answer,but a question .And if you are referring about cellForRowAtIndexPath,you need a UITableView not a cell.