Iphone 如何使多维数组在tableview中工作

Iphone 如何使多维数组在tableview中工作,iphone,swift,xcode,Iphone,Swift,Xcode,我正在创建一个iOS Swift 3应用程序,在这个应用程序中,我有一个包含来自API的数据的tableview。我想在tableview中显示这些数据,但我想按名字分组。我已经设法首先将数据分组(见下文),但xCode抱怨XXX 这是各节的声明: var sections = [String: [User]]() self.sections = Dictionary(grouping: self.contacts.filter({ !$0.firstname!.isEmpty })) {

我正在创建一个iOS Swift 3应用程序,在这个应用程序中,我有一个包含来自API的数据的tableview。我想在tableview中显示这些数据,但我想按名字分组。我已经设法首先将数据分组(见下文),但xCode抱怨XXX

这是各节的声明:

var sections = [String: [User]]()
self.sections = Dictionary(grouping: self.contacts.filter({ !$0.firstname!.isEmpty })) {
                $0.firstname!.prefix(1).capitalized
            }
["D": [motto.User(id: Optional(1), firstname: Optional("Dan"), lastname: Optional("Meeler"), email: Optional("time@example.com"))], "M": [coiqo.User(id: Optional(3), firstname: Optional("Mia"), lastname: Optional("Kallas"), email: Optional("mia@ka.com"))]]
Cannot subscript a value of type '[String : [User]]' with an index of type 'Int'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }
这是我对数据进行分组的方式:

var sections = [String: [User]]()
self.sections = Dictionary(grouping: self.contacts.filter({ !$0.firstname!.isEmpty })) {
                $0.firstname!.prefix(1).capitalized
            }
["D": [motto.User(id: Optional(1), firstname: Optional("Dan"), lastname: Optional("Meeler"), email: Optional("time@example.com"))], "M": [coiqo.User(id: Optional(3), firstname: Optional("Mia"), lastname: Optional("Kallas"), email: Optional("mia@ka.com"))]]
Cannot subscript a value of type '[String : [User]]' with an index of type 'Int'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }
这是我的输出:

var sections = [String: [User]]()
self.sections = Dictionary(grouping: self.contacts.filter({ !$0.firstname!.isEmpty })) {
                $0.firstname!.prefix(1).capitalized
            }
["D": [motto.User(id: Optional(1), firstname: Optional("Dan"), lastname: Optional("Meeler"), email: Optional("time@example.com"))], "M": [coiqo.User(id: Optional(3), firstname: Optional("Mia"), lastname: Optional("Kallas"), email: Optional("mia@ka.com"))]]
Cannot subscript a value of type '[String : [User]]' with an index of type 'Int'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }
这是我得到的错误:

var sections = [String: [User]]()
self.sections = Dictionary(grouping: self.contacts.filter({ !$0.firstname!.isEmpty })) {
                $0.firstname!.prefix(1).capitalized
            }
["D": [motto.User(id: Optional(1), firstname: Optional("Dan"), lastname: Optional("Meeler"), email: Optional("time@example.com"))], "M": [coiqo.User(id: Optional(3), firstname: Optional("Mia"), lastname: Optional("Kallas"), email: Optional("mia@ka.com"))]]
Cannot subscript a value of type '[String : [User]]' with an index of type 'Int'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }
在tableview的此函数中:

var sections = [String: [User]]()
self.sections = Dictionary(grouping: self.contacts.filter({ !$0.firstname!.isEmpty })) {
                $0.firstname!.prefix(1).capitalized
            }
["D": [motto.User(id: Optional(1), firstname: Optional("Dan"), lastname: Optional("Meeler"), email: Optional("time@example.com"))], "M": [coiqo.User(id: Optional(3), firstname: Optional("Mia"), lastname: Optional("Kallas"), email: Optional("mia@ka.com"))]]
Cannot subscript a value of type '[String : [User]]' with an index of type 'Int'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }
如何使此数组在tableview中工作

更新

我只需将此添加到dasblinkenlight答案中,就成功了

if(groupKeys.count > 0) {
            return sections[groupKeys[section]]!.count
        } else {
            return sections.count
        }

你的
self.sections
Dictionary
,而Dictionary是无序的

根据API结果创建用户组后,创建一个由组键组成的单独数组:

let groupKeys : [String] = Array(self.sections.keys)
按照您希望分区显示的方式(字母顺序、逆字母顺序、自然顺序等)对该数组进行排序

现在您可以这样编写函数:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[groupKeys[section]].count
}

let key=sections.keys.sorted()[section];返回节[key]。计数
?很遗憾,它不起作用。我得到了“线程1:致命错误:索引超出范围”哪一行?我建议的那个?我想你在别的地方有问题。另外,我要做
var数组=[[String:[User]]]();对于key.enumerated(){array.append([aKey:dict[aKey]!])}
中的(u,aKey),我将使用该
array
填充您的表视图。不幸的是,我得到了线程1:致命错误:索引超出了range@JonathanClark我没有检查,但我认为您的“线程1:致命错误:索引超出范围”在别处,例如,在
cellForRowAtIndexPath:
中,因为设置正确的
NumberOfRowInSection
也意味着为它调整
CellForRowAtexPath:
。不,我在NumberOfRowInSection函数中得到它。或者至少在那条线上有xcode点。