Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIFT线程1:致命错误:索引超出范围:节和;排_Swift_Swift5 - Fatal编程技术网

SWIFT线程1:致命错误:索引超出范围:节和;排

SWIFT线程1:致命错误:索引超出范围:节和;排,swift,swift5,Swift,Swift5,需要一些帮助来获取分组行以填充其各自的部分。我让它工作了一秒钟,但试图调整代码以消除重复和丢失的行。从那以后,我一直在犯“致命错误” 这是一个实践项目。按部门分组的用户目录。使用Firebase提取用户列表。按部门对它们进行分组,然后填充一个表 //MARK: - CELLS override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

需要一些帮助来获取分组行以填充其各自的部分。我让它工作了一秒钟,但试图调整代码以消除重复和丢失的行。从那以后,我一直在犯“致命错误”

这是一个实践项目。按部门分组的用户目录。使用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:致命错误:索引超出范围”

几点:

表视图或集合视图应显示数据数组(一维或二维)。字典是无序的,因此它不适用于这两种情况的数据模型

您当前的代码正在通过cellForRowAt对数据模型进行计算密集型重建。您正在使用
Dictionary(grouping:)
构建一个字典,然后在每个
tableView(u:cellForRowAt:)
上从这些字典创建数组。您首先要创建一个字典,所以每次节的顺序都可能不同。不要那样做。(这可能是导致崩溃的原因之一,因为在每次通过“tableView”(u:cellForRowAt:)时,部门的排列顺序可能不同,因此每个部分中的用户数量可能不同

另外,在
tableView(u∶cellForRowAt:)
中构建数据模型没有任何意义,因为系统首先调用
numberOfSections(in:)
,然后调用
tableView(∶numberofrowsinssection:)
,然后开始调用节标题和单元格。您不显示
numbersections(in:)
tableView(uquo;numberOfRowsInSection:)
方法,但我不知道它们如何返回正确的信息,因为它们的数据模型在第一次调用时没有设置

我建议在第一次设置表视图时定义一个节结构数组并填充一次

struct SectionStruct: {
  let sectionTitle: String //Department name?
  let sectionItems: [User]
}

var sections = [SectionStruct]() //populate this as described below
然后,在第一次设置表视图时,按部门名称对用户数组进行分组,构建一个节结构数组,对该数组进行排序,并将其用作数据模型

现在,在numberOfSections方法中,将返回
sections.count

rowsincreation
将返回
sections[section]。count

您可以将用于构建数据模型的代码放入
目录数组的
didSet
方法中:

var directoryArray: [User] {
  didSet {
    //Code to group your users by department, build the section structures, and sort them

    tableView?.reloadData()  //Tell the table view to reload
  }
注意:上面的代码更像是伪代码,而不是您可以复制到项目中的实际代码。它旨在为您指明正确的方向。不要试图将其复制到项目中,如果(当)它无法编译,请抱怨

如果您需要重构代码的更多帮助,您将不得不发布数据结构(例如
directoryArray
)和
numberOfSections(in:)
表视图(uu:numberofrowsinsSection:)
,以及
表视图(u:titleForHeaderInSection:)
数据源方法。编辑您的问题,将更改后的代码添加到底部。不要尝试在注释中发布代码。代码未格式化,因此不可读