2017-12-17 27 views
1

をテーブルのデータソースを設定するとき、私は現在、単一のtableViewのための複数の細胞型を作る方法を学んでいると私はスウィフトでUITableView 4.エラー特定のクラスに

のためのデータソースを設定しようとすると、私はエラーを取得します次のようなエラーが発生します

'ProfileViewModel.Type'タイプの値を 'UITableViewDataSource'と入力できません。

私はこのエラーメッセージを取得するコードは、コードに関する詳細は以下の通りです。この1

tableView?.dataSource = ProfileViewModel 

です。このクラスは元のViewControllerクラス外ですが、私はUITableViewDataSourceでクラスを宣言しました。

class ProfileViewModel: NSObject, UITableViewDataSource { 
    var items = [ProfileViewModelItem]() 

    init(profile: Profile) { 
     super.init() 
     guard let data = dataFromFile(filename: "ServerData") else { 
      return 
     } 

     let profile = Profile(data: data) 

     if let name = profile.fullName, let pictureUrl = profile.pictureUrl { 
      let nameAndPictureItem = ProfileViewModelNameAndPictureItem(pictureUrl: pictureUrl, userName: name) 
      items.append(nameAndPictureItem) 
     } 
     if let about = profile.about { 
      let aboutItem = ProfileViewModelAboutItem(about: about) 
      items.append(aboutItem) 
     } 
     if let email = profile.email { 
      let dobItem = ProfileViewModelEmailItem(email: email) 
      items.append(dobItem) 
     } 
     let attributes = profile.profileAttributes 
     if !attributes.isEmpty { 
      let attributesItem = ProfileViewModelAttributeItem(attributes: attributes) 
      items.append(attributesItem) 
     } 
     let friends = profile.friends 
     if !profile.friends.isEmpty { 
      let friendsItem = ProfileViewModeFriendsItem(friends: friends) 
      items.append(friendsItem) 
     } 
    } 

} 

extension ProfileViewModel { 
    func numberOfSections(in tableView: UITableView) -> Int { 
     return items.count 
    } 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items[section].rowCount 
    } 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     // config 
    } 
} 

私はどうしたらよいですか?

+0

'init'は' Profile'を渡すと意味をなさないが、そのパラメータを無視するように見えます。あなたはそのレビューをしたいかもしれません... – Rob

+0

あなたのコメントをありがとうRob!私はそれに気付かなかった。私はそれを修正するつもりです。私はinitの後にプロファイルを宣言する必要はないと思う。 – George

答えて

0

を意味だと思います。したがって、あなたはあなたのProfileViewModelインスタンス化する必要があります:私はしませんでした、

class ViewController { 
    @IBOutlet weak var tableView: UITableView! 

    var profileViewModel: ProfileViewModel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let profile = ... 
     profileViewModel = ProfileViewModel(profile: profile) // it doesn't look like you need this parameter, so remove it if you don't really need it 

     tableView?.dataSource = profileViewModel 

     ... 
    } 
} 

注意を直接次

tableView?.dataSource = ProfileViewModel(profile: ...) 

問題のtableViewはへの強い参照を保持していないということですが、そのdataSource、これは割り当て解除されます。あなた自身の強い参照を保持し、その参照を使用する必要があります。

+0

コメントありがとうございました!私は参考資料について知りませんでした。私はあなたの方法を試して、それは働いた。 – George

1

このライン:

tableView?.dataSource = ProfileViewModel 

あなたはtableView.dataSourceタイプを割り当てようとしています。テーブルビューのデータソースは、おそらくタイプにはできません。 UITableViewDataSourceに従うタイプのオブジェクトである必要があります。

私はあなたがクラスを参照しているが、あなたはそのクラスのインスタンスを参照する必要が

tableView?.dataSource = self 
+0

コメントありがとうございます!うーん、私は参照してください。私は先月勉強を始めて以来、正直言ってタイプとオブジェクトの違いを理解していませんが、タイプはオブジェクトのタイプに似ていて、オブジェクトと思われるデータソースにタイプを渡しました。 – George

関連する問題