如何解决Swift:将TableView节与对象数组一起使用错误:type的值没有下标// // type的值没有成员“ count”
问题很简单。我知道如何利用TableViews以及如何利用numberOfSections
函数。
现在的问题是,我正在从Firebase数据库中以对象数组的形式检索数据。
这是members = [PaymentTeam]()
所在的类,如下所示:
class PaymentTeam {
var key: String?
let memberJob: String?
let memberName: String?
let memberID:String?
let projectCreated:String?
let projectName: String?
let confirmedBool: Bool?
let paidBool: Bool?
init(memberJob: String,memberName: String,memberID:String,confirmedBool: Bool,paidBool: Bool,projectCreated:String,projectName: String) {
self.memberName = memberName
self.memberJob = memberJob
self.memberID = memberID
self.projectCreated = projectCreated
self.confirmedBool = confirmedBool
self.paidBool = paidBool
self.projectName = projectName
}
var dictValue: [String: Any] {
return ["memberName" : memberName as Any,"memberJob" : memberJob as Any,"memberID": memberID as Any,"projectCreated": projectCreated as Any,"confirmedBool": confirmedBool as Any,"paidBool": paidBool as Any,"projectName": projectName as Any
]
}
init?(snapshot: DataSnapshot) {
guard let dict = snapshot.value as? NSDictionary,let memberJob = dict["memberJob"] as? String,let memberName = dict["memberName"] as? String,let projectCreated = dict["projectCreated"] as? String,let projectName = dict["projectName"] as? String,let memberID = dict["memberID"] as? String,let confirmedBool = dict["confirmedBool"] as? Bool,let paidBool = dict["paidBool"] as? Bool
else {return nil}
self.memberJob = memberJob
self.memberName = memberName
self.projectCreated = projectCreated
self.projectName = projectName
self.memberID = memberID
self.confirmedBool = confirmedBool
self.paidBool = paidBool
}
}
并按以下方式进行检索:
static func chosenTeam(for user: User,completion: @escaping ([PaymentTeam]) -> Void) {
let ref = Database.database().reference().child("selectedTeams").child(User.current.uid)
ref.observe(DataEventType.value,with: { (snapshot) in
guard let snapshot = snapshot.children.allObjects as? [DataSnapshot] else {
return completion([])
}
let chosenTeam = snapshot.reversed().compactMap(PaymentTeam.init)
completion(chosenTeam)
})
}
现在的问题是:
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return members[section].count
}
我收到错误消息:Value of type 'PaymentTeam' has no member 'count'
,我只能在不同的类成员之间进行选择,例如members.memberID
对于功能:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let member = members[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "ChosenTeamNaMetableViewCell") as! ChosenTeamNaMetableViewCell
cell.memberNameLabel.text = member.memberName
return cell
}
我收到以下错误消息:
Value of type 'PaymentTeam' has no subscripts
[indexPath.section]
带有下划线。
也许有人可以帮助我弄清楚如何在该members
数组中构造Firebase数据,以实际使用numberofSections
这是带有Tableview的ViewController的完整代码:
class PaymentHomeViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var headerSeperator: UIView!
var members = [PaymentTeam]()
override func viewDidLoad() {
super.viewDidLoad()
setUpUI()
UserService.chosenTeam(for: User.current) { (chosenTeam) in
self.members = chosenTeam
for value in self.members {
self.projectTitles.append(value.projectName ?? "Error")
}
self.tableView.reloadData()
}
}
func setUpUI() {
Utilities.addShadowtoView(headerSeperator)
}
extension PaymentHomeViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = "Header"
label.backgroundColor = UIColor.lightGray
}
func numberOfSections(in tableView: UITableView) -> Int {
return members.count
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return members[section].count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let member = members[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "ChosenTeamNaMetableViewCell") as! ChosenTeamNaMetableViewCell
cell.memberNameLabel.text = member.memberName
return cell
}
}
// MARK: - UITableViewDelegate
extension PaymentHomeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 78
}
}
非常感谢您的帮助。 :)
解决方法
在numberOfRowsInSection
中,您无意间尝试在此处访问类型为“ PaymentTeam”的名为“ count”的成员:
return members[section].count
“成员”是一维数组,因此members[section]
返回的类型为PaymentTeam
的对象,该对象不是数组,因此没有计数。更改为:
return members.count
现在,您的第二个错误出现在此行:
let member = members[indexPath.section][indexPath.row]
同样,“成员”是一维数组,而不是二维数组。您正在尝试使用array[index1][index2]
访问二维数组的成员。这没有意义,因为PaymentTeam
不是数组。您可以在this StackOverflow线程中快速找到有关二维数组的更多信息。更改为:
let member = members[indexPath.row]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。