2017-03-19 9 views
-1

私は、タイトルとメモを必要とするiOSノートを作成しています。私はtextField私のタイトルとtextView私のメモです。次に、これらの2つを配列に追加し、tableViewに追加して、タイトルと注釈を表示します。私が使用しているコードは、すべてのノートをtableViewに追加し、すべてのタイトルで同じものを表示します。私はそれにdictionaryを使用しなければならないことを知っていますが、どうすればそれを実装できますか?これはtextViewtextFieldを持っているVCのコードである辞書に値を追加するにはどうすればよいですか?

@IBAction func addItem(_ sender: Any) 
{ 
     list.append(textField.text!) 
     list2.append(notesField.text!) 
} 
listlist2が空である

arraytableView私はそのVCのためのlist2の内容とコードを表示するtextViewを持っている拡張可能な細胞を持っています次のとおりです。

override func awakeFromNib() { 
    super.awakeFromNib() 

    textView.text = list2.joined(separator: "\n") 

} 
+1

スウィフト言語ガイドをお読みください。 – Alexander

答えて

1
for title in dict.keys { 
    print("The title is: \(title)") 
} 
// and 
for body in dict.values { 
    print("The body is: \(body)") 
} 

だけ

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) 
} 

辞書の配列を取るそして、ちょうど2つのtableViewCellクラスtitleLableで口と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 
    } 

ノートを作ることでちょうどのtableViewデータソース方式で、それを移入:私はそれをテストしていませんコード上で、しかしそれが確実に動作することを願っています。 すべて最高です。

0

あなたが割り当てることにより、スウィフトで辞書に要素を追加します。

var dict = [String : String]() 

let title = "My first note" 
let body = "This is the body of the note" 

dict[title] = body // Assigning the body to the value of the key in the dictionary 

// Adding to the dictionary 
if dict[title] != nil { 
    print("Ooops, this is not to good, since it would override the current value") 

    /* You might want to prefix the key with the date of the creation, to make 
    the key unique */ 

} else { 
// Assign the value of the key to the body of the note 
    dict[title] = body 
} 

あなたはできるそれからタプルを使用して、辞書をループ:

for (title, body) in dict { 
    print("\(title): \(body)") 
} 

あなたが身体やタイトルにのみ関心がある場合、あなたは、単にこのよう_にタイトルや本文を置き換えることにより、他を無視することができますが。

for (_, body) in dict { 
    print("The body is: \(body)") 
} 
// and 
for (title, _) in dict { 
    print("The title is: \(title)") 
} 

タイトル/本体はまた、キーによってアクセスまたは辞書の特性値ことができる:

関連する問題