2017-04-17 7 views
2

프로필 클래스를 만들었으며이 뷰 컨트롤러가 사용자가 제공 한 정보에서 결제 프로필 개체를 저장하고 사용자가 제공 한 이름 아래 목록의 TableView에 프로필을 표시하려고합니다. 필자는이 배열을 시도했지만 프로파일 이름을 키로 만들 수 있기 때문에 사전이 더 합리적인 것처럼 보이게하기로 결정했습니다. 그리고 그 테이블에 표시된Swift 3 사전에서 TableView 채우기?

그러나 프로필로 테이블을 채우는 데 문제가 있습니다.

어떤 도움이라도 대단히 감사 드리며 어떤 제안을 시도해도 좋습니다. 나는 사진과 코드

Picture

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 
} 
} 
+1

표에는 정렬 된 데이터 (즉, 배열)가 표시됩니다. 데이터가 사전 형식 인 경우 어떻게 든 배열로 변환해야합니다. 분명한 것은 알파벳순으로 키를 사용하여 사전 항목을 정렬하는 것입니다. –

+0

배열로 바꾸라고 제안 해 주시겠습니까? 아니면 이것을 표시 할 수있는 다른 방법이 있습니까? 응답 주셔서 감사합니다 @ NicolasMiari –

+0

반드시 그렇지는 않습니다. 테이블 뷰뿐만 아니라 앱 전체에서의 사용에 기반하여 데이터에 가장 편리한 형식이 무엇인지 고려해야합니다. –

답변

0

당신이 할 수있는 것은 당신의 사전에 인덱스 배열을 사용하는 것입니다

enter image description here

. 사전의 키 배열 속성에서 배열을 생성 할 수 있습니다. 여기에서 필자는 키로 정렬했지만 프로필 이름이나 더 잘 이해할 수있는 다른 필드를 기준으로 정렬하려면 클로저를 제공해야합니다.

class V: UIViewController { 
     @IBOutlet weak var tableView: UITableView! 
     var profiles: [String: Profile] = [:] 
     var indices: [String] = [] 
     override func viewDidLoad() { 
      indices = profiles.keys.sorted() 
     } 
    } 

    extension V: UITableViewDataSource { 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return indices.count 
     } 
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyCell.self), for: indexPath) as MyCell 
      cell.profile = profiles[indicies[indexPath.row]]! 
      return cell 
     } 
    } 
+0

NSTableViews와 같은 것이겠습니까? –

+0

OS X에 있었기 때문에 UI 대신 NSViewController가 필요합니까? –

+0

정확히는 NSTableViewDataSource 메서드를 사용하기 위해이를 수정해야합니다 (UITableView는 1 차원이지만 NSTableView는 행과 열을 가짐)하지만 개념적으로는 같은 것입니다 : 데이터는 사전에 보관되어 있습니다. 테이블이 데이터를 표시하는 순서를 나타내는 해당 사전에 대한 키 배열. –