【发布时间】:2017-12-17 10:49:05
【问题描述】:
我目前正在学习如何为单个 tableView 创建多个单元格类型,当我尝试在 Swift 4 中为 UITableView 设置数据源时出现错误。
我在下面遇到这样的错误
无法将类型“ProfileViewModel.Type”的值分配给类型“UITableViewDataSource?”
我收到此错误消息的代码是这个
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
}
}
有人可以帮帮我吗?
【问题讨论】:
-
不相关,您的
init在您传递Profile时不太有意义,但您似乎忽略了该参数。你可能想回顾一下…… -
感谢您的评论罗!我没有注意到这一点。我要修好它。我想我不需要在初始化之后声明配置文件。
标签: ios swift uitableview tableview