2

So I am building a notes app and have tried everything but can not figure this issue out. I have 3 UIViewController's. When you click a button on the first UIViewController, it shoots you over to the third one which consist of a UITextView, UITextField and a Static UILabel which gets updated with some information. When you fill out these fields and tap the back button it brings you to the second view controller which is a table that gets updated with this information.

The issue is: when I tap the UITableViewCell it loads the information back to the third view controller so the user can edit his notes but when I come back to the UITableView it creates a brand new cell instead of just updating the old one.

If I could just update my array with the same object I sent back to be edited by the user I think this issue would be solved but I have no idea how to do this. Thanks for the help!

VC2 - this is how I am sending my data from the tableView to the textView Back

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let nextView = segue.destinationViewController as! TextViewController

    guard let indexPath = self.tableView.indexPathForSelectedRow else {
        print("Didnt work")
        return
    }

    let dataToSendBackToBeEdited = textViewObjectData[indexPath.row]

    print(dataToSendBackToBeEdited.dreamText)
    print(dataToSendBackToBeEdited.dreamTitle)
    print(dataToSendBackToBeEdited.dreamType)
    print(dataToSendBackToBeEdited.description)
    print(dataToSendBackToBeEdited)

    nextView.objectSentFromTableViewCell = dataToSendBackToBeEdited
}

This is how I am saving the information the the user taps back to go to the tableView

func determineSave() {

    guard var savedDreamArray = NSKeyedUnarchiver.unarchiveObjectWithFile(TextViewController.pathToArchieve.ArchiveURL.path!) as? [Dream] else {

        //First dream object saved
        let dreamObject = Dream(dreamTitle: titleForDream.text!, dreamType: typeOfDreamLabel.text!, dreamText: textView.text, currentDate: NSDate())
        dreamArray.append(dreamObject)
        NSKeyedArchiver.archiveRootObject(dreamArray, toFile: pathToArchieve.ArchiveURL.path!)

        return
    }

    //print(savedDreamArray.count)
    //print(savedDreamArray.first!.dreamTitle)


    let dreamObject = Dream(dreamTitle: titleForDream.text!, dreamType: typeOfDreamLabel.text!, dreamText: textView.text!, currentDate: NSDate())


    savedDreamArray.append(dreamObject)
    NSKeyedArchiver.archiveRootObject(savedDreamArray, toFile: pathToArchieve.ArchiveURL.path!)

}
R Menke
  • 8,183
  • 4
  • 35
  • 63
JakeC
  • 231
  • 1
  • 2
  • 9
  • @bobby - I am appending my objects to an array when I tap the back button. I just can't figure out how to get that index and that specific object when're user edited the note. I understand the theory behind it but just can't write it myself. Any help would be great! – JakeC Dec 06 '15 at 18:39

2 Answers2

2

I was having this issue as well. Came in here, and got the answer. The array!

I was appending the array as well, which apparently was causing duplicate cells to appear.

I just reset my array back to empty before I retrieved the data again and reloaded the table.

I'm using Firebase so my code looks like this:

DataService.instance.dateProfileRef.observeEventType(FIRDataEventType.Value) { (snapshot: FIRDataSnapshot)
        in

        //have to clear the array here first before reloading the data. otherwise you get duplicates
        self.posts = []    //added this line
        if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {

            for snap in snapshot {
                let snapUserID = (snap.childSnapshotForPath("userID").value!)

                if snapUserID as? String == USER_ID {
                if let profileDict = snap.value as? Dictionary<String, AnyObject> {

                    let key = snap.key
                    let post = Post(postKey: key, postData: profileDict)

                        self.posts.append(post)
Matt Wells
  • 21
  • 2
0

You do a .append() on your array, this will add a new cell.

You must find the index of the object you want to update and then assign it: array[index] = newObject

bobby
  • 105
  • 1
  • 10