I'm making an iOS notes taking app which requires a title and notes. I have a textField
for my title and textView
for my notes. I then add these two in an array and append them in my tableView
where we can see the title and notes. The code I'm using appends all my notes in tableView
and show the same for all the title. I know I have to use a dictionary
for that but how can I implement that ? This is the code of VC which has the textView
and textField
@IBAction func addItem(_ sender: Any)
{
list.append(textField.text!)
list2.append(notesField.text!)
}
where list
and list2
are empty array
In my tableView
I have expandable cells which have a textView
to show the content of list2
and the code for that VC is :
override func awakeFromNib() {
super.awakeFromNib()
textView.text = list2.joined(separator: "\n")
}
Just take an array of dictionary
var arrOfDict = [[String :AnyObject]]()
var dictToSaveNotest = [String :AnyObject]()
@IBAction func addItem(_ sender: Any)
{
dictToSaveNotest .updateValue(textField.text! as AnyObject, forKey: "title")
dictToSaveNotest .updateValue(NotesField.text! as AnyObject, forKey: "notesField")
arrOfDict.append(dictToSaveNotest)
}
And just populate it in the tableView Data source method By just make two outlet in the tableViewCell Class titleLable and notesLabel
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! yourTableViewCell
cell.titleLabel.text = arrayOfDict[indexPath.row]["title"] as! String!
cell.notesLabel.text = arrayOfDict[indexPath.row]["notesField"] as! String!
return cell
}
Note : I have not tested it on the code , but hope it will work definitely. All the best .