【发布时间】:2019-06-09 13:55:31
【问题描述】:
需要一些帮助来获得分组行以填充它们各自的部分。我让它工作了一秒钟,但试图调整代码以消除重复和丢失的行。从那以后,我一直在收到“致命错误”
这是一个练习项目。按部门分组的用户目录。使用 Firebase 拉取用户列表。按部门对它们进行分组,然后填充一个表格。
//MARK: - CELLS
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "directoryCell", for: indexPath) as! TableViewCell
let departmentSections = Dictionary(grouping: directoryArray) { (user) -> String in
return user.department
}
//ROW & SECTION SETUP
let rowsInSection = Array(departmentSections.values)
//let usersInRowData = rowsInSection[indexPath.section]
let users = rowsInSection[indexPath.section][indexPath.row]
//USER CELL INFO
let profilePic = users.profilePic // PROFILE PHOTO GET AND DISPLAY
let url = URL(string: profilePic); URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
cell.profilePicView.image = UIImage(data: data!)
cell.layoutSubviews()
}
}).resume()
//CELL LABELS
cell.displayNameLabel.text = users.displayName
cell.emailLabel.text = users.email
cell.officeLabel.text = users.office
cell.titleLabel.text = users.title
cell.profilePicView.layer.masksToBounds = true;
//cell.profilePicView.contentMode = UIView.ContentMode.scaleAspectFill;
//cell.departmentLabel.text = users.department
//cell.reportsToLabel.text = users.directReports
//cell.extLabel.text = users.ext
cell.layoutSubviews()
return cell
}
我希望用户按部门分组。我得到的只是:“线程 1:致命错误:索引超出范围”
【问题讨论】: