私はProfileクラスを作成しました。このviewcontrollerに、ユーザーから提供された情報から課金プロファイルオブジェクトを保存して、ユーザーが与えた名前のリストのTableViewにプロファイルを表示しようとしています。私は配列でこれを試みましたが、私はプロファイル名をキーにすることができるので、辞書がより合理的に見える理由のためにそれを行かせることに決めました。テーブルに表示されていますSwift 3 DictionaryからTableViewを読み込む?
しかし、私はプロファイルでテーブルにデータを入力するのに苦労しています。
ご協力いただければ幸いです。ご提案をお待ちしております。私は絵やコード
var dictionary = [String:Profile]()
let profile1 = Profile()
profile1.firstNameB = firstNameBill
profile1.lastNameB = lastNameBill
profile1.emailB = emailBill
profile1.phoneB = phoneBill
profile1.address1B = address1Bill
profile1.address2B = address2Bill
profile1.zipB = zipBill
profile1.cityB = cityBill
profile1.firstNameS = firstNameShip
profile1.lastNameS = lastNameShip
profile1.phoneS = phoneShip
profile1.address1S = address1Ship
profile1.address2S = address2Ship
profile1.zipS = zipShip
profile1.cityS = cityShip
dictionary.updateValue(profile1, forKey: pName)
}
UPDATEを提供します:まだ少し混乱し、私は次のコードを使用して新しいファイルを作成し、これを取得しています。あなたは何ができるか
class ProfileTable: NSViewController {
@IBOutlet weak var tableView: NSTableView!
var profiles: [String: Profile] = [:]
var indices: [String] = []
override func viewDidLoad() {
indices = profiles.keys.sorted()
}
}
extension ProfileTable: NSTableViewDataSource {
func tableView(_ tableView: NSTableView, numberOfRowsInSection section: Int) -> Int {
return indices.count
}
func tableView(_ tableView: NSTableView, cellForRowAt indexPath: IndexPath) -> NSTableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyCell.self), for: indexPath) as MyCell
cell.profile = profiles[indices[indexPath.row]]!
return cell
}
}
表には、順序付けられたデータ(配列)が表示されます。データが辞書形式の場合は、何らかの形で配列に変換する必要があります。明らかにするべきことは、アルファベット順にキーで辞書項目をソートすることです。 –
アレイ全体に切り替えることを提案しますか?それとも私がこれを表示するための他の方法がありますか?返信ありがとう@NicolasMiari –
必ずしもそうではありません。あなたは、テーブルビューだけでなく、あなたのアプリケーションの周りのすべての用途に基づいて、データにとって最も便利なフォーマットを検討する必要があります。 –