简体   繁体   中英

How do I get the table view to not save notes when there is no text in the text view?

Im making a notes app and Im trying to figure out how to not save a note in the tableview when there is no text. Right now when I press the save button and theres no text in the text view it still saves it in the table view with a blank cell. Can someone help me with this problem? Thank you! Here is the code Im using when I want to save a note to a table view.

    @IBAction func saveNotes(sender: AnyObject) {
    print("SAVE ME")

    var userDefaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

    var itemList: NSMutableArray? = userDefaults.objectForKey("itemList") as? NSMutableArray


    var dataSet: NSMutableDictionary = NSMutableDictionary()
    dataSet.setObject(textView.text, forKey: "text")

    if ((itemList) != nil) {


        var newMutableList: NSMutableArray = NSMutableArray()

        for dict: AnyObject in itemList! {

            newMutableList.addObject(dict as! NSDictionary)
        }


        userDefaults.removeObjectForKey("itemList")

        newMutableList.addObject(dataSet)
        userDefaults.setObject(newMutableList, forKey: "itemList")





    } else {
        userDefaults.removeObjectForKey("itemList")
        itemList = NSMutableArray()
        itemList!.addObject(dataSet)

        userDefaults.setObject(itemList, forKey: "itemList")


    }

    userDefaults.synchronize()
    self.navigationController?.popToRootViewControllerAnimated(true)

    self.navigationController?.setNavigationBarHidden(false, animated: true)
    self.navigationController?.toolbarHidden = false



}

You need to check the string length before you add it in your dataSet which I believe is used to render cells in your table view. Something like this:

let characterCount = textView.text.characters.count // This is for Swift 2. For Swift use count(textView.text)

if characterCount > 0 {
    dataSet.setObject(textView.text, forKey: "text")
}

EDIT: Post OP comments

As I suggested, you should disable your save button until there is some text in your text view. This is how you need to do this:

Step 1: Set your view controller delegate of your text view.

self.textView.delgate = self

Step 2: Implement following function to enable/disable save button.

func textView(textView: UITextView, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
  self.button.enabled = (textView.text.characters.count > 0)
  return true
}

PS: As a side note, you may want to safeguard your self from user typing spaces. I am not adding that code here trusting you might already know.

You need to check textView text length before adding text to your table vie w data source dataSet . So give an text length checking condition and avoid doing other task. As like as

if textView.text.characters.count > 0 {
     dataSet.setObject(textView.text, forKey: "text")
     // do rest of work
} else
{
     // show an alert for empty text
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM