Ios tableView中最常见的数组元素

Ios tableView中最常见的数组元素,ios,arrays,uitableview,swift2,xcode7,Ios,Arrays,Uitableview,Swift2,Xcode7,首先,我想说我是iOS(Swift 2.0)编码的初学者。所以我有一个数组,我想先用最常见的元素排序,最后用最不常见的元素排序。然后我想将其插入UITableView,其中最常见的项目应显示在顶部,最不常见的项目应显示在底部。因此,如果我在数组中有一个名为“Food”的项目,它出现了3次,那么它应该位于一个名为“Candy”的项目之上,该项目出现了2次。谢谢 编辑: 我很抱歉提出了一个如此糟糕的问题,正如我所说的,我是一个编程新手,所以我也不是最擅长解释我的情况的人。但这次我会尽量更好地描述我的

首先,我想说我是iOS(Swift 2.0)编码的初学者。所以我有一个数组,我想先用最常见的元素排序,最后用最不常见的元素排序。然后我想将其插入UITableView,其中最常见的项目应显示在顶部,最不常见的项目应显示在底部。因此,如果我在数组中有一个名为“Food”的项目,它出现了3次,那么它应该位于一个名为“Candy”的项目之上,该项目出现了2次。谢谢

编辑: 我很抱歉提出了一个如此糟糕的问题,正如我所说的,我是一个编程新手,所以我也不是最擅长解释我的情况的人。但这次我会尽量更好地描述我的情况。首先,我想先用最常用的元素对数组进行排序,然后用第二个最常用的元素进行排序,以此类推。John提供的答案帮助我做到了这一点,之后我设法将其放入如下的tableView中:

 var counts : Dictionary<String, Int> = Dictionary<String, Int>()


override func viewDidLoad() {

    for (_, value) in prioLista.enumerate() {

        if let count = counts[value] {

            counts[value] = count + 1

        }
        else {
            counts[value] = 1
        }

    }

    prioLista.sortInPlace { (first, second) -> Bool in

        let firstCount = counts[first]
        let secondCount = counts[second]

        return firstCount > secondCount
    }

  navigationItem.title = "Spara"

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return prioLista.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellster = tableView.dequeueReusableCellWithIdentifier("cellster")


    cellster?.textLabel?.text = prioLista[indexPath.row]

    return cellster!
}
var计数:Dictionary=Dictionary()
重写func viewDidLoad(){
对于prioLista.enumerate()中的(_,value){
如果let count=计数[值]{
计数[值]=计数+1
}
否则{
计数[值]=1
}
}
(第一,第二)->Bool in
让firstCount=计数[第一]
让secondCount=计数[秒]
返回firstCount>secondCount
}
navigationItem.title=“Spara”
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回prioLista.count
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cellster=tableView.dequeueReusableCellWithIdentifier(“cellster”)
cellster?.textLabel?.text=prioLista[indexPath.row]
回监狱!
}
代码中包含的变量prioLista是另一个文件中的数组。通过按下按钮,您可以向其中添加字符串

这就是我现在所处的位置,正如图片所示。tableView按照我的要求对元素进行排序。但是,我希望每个不同的对象只有一行。因此,如果数组包含的“Mat”比“Godis”多,我仍然希望“Mat”在“Godis”之上,但我希望它们各占一行。到目前为止,tableView显示了我数组中的每一项,相反,我希望它删除重复项,但仍然将最常见的项优先于不太常见的项。我知道这听起来可能很奇怪,我想删除项目,但仍然使用约翰提供的功能,但如果有一个这样做的方式,我会很高兴

现在总结一下我真正想要的是什么,所以我尽可能清楚地说明这一点;在我链接的图片中,我的对象是按它们的常见程度排序的,我想知道是否有一种方法可以只显示一个相同的对象,但仍然以这种方式对数组进行排序。因此,如果“Mat”多于“Godis”,则应显示为:

第一排:垫子第二排:戈迪斯

在桌面视图中


我希望我已经足够清楚了,作为提醒,我只是一个试图学习编码的初学者:)

可能有几种方法可以实现这一点,但这里有一个例子。首先获取字符串在数组中出现的次数,然后将其存储在字典中。然后使用该字典对数组进行排序:

let arr = ["Food", "Candy", "Food", "Candy", "Food"]

var counts : Dictionary<String, Int> = Dictionary<String, Int>()

for (index, value) in arr.enumerate() {

    if let count = counts[value]{

        counts[value] = count + 1

    }
    else{
        counts[value] = 1
    }

}

let sorted = arr.sort { (first, second) -> Bool in

    let firstCount = counts[first]
    let secondCount = counts[second]

    return firstCount > secondCount

}
让arr=[“食品”、“糖果”、“食品”、“糖果”、“食品”]
变量计数:Dictionary=Dictionary()
用于数组枚举()中的(索引、值){
如果let count=计数[值]{
计数[值]=计数+1
}
否则{
计数[值]=1
}
}
让sorted=arr.sort{(第一,第二)->Bool-in
让firstCount=计数[第一]
让secondCount=计数[秒]
返回firstCount>secondCount
}

请注意,此处不能给出完整答案。请描述一下你做了多少,在哪里结巴。非常感谢!我现在只有一个问题,当我试图将数据传递到我的tableview时,结果是这样的:[“食物”:1,“糖果”:1]当我向其中添加1个糖果和1个食物时。这是我的全部代码,正如我所说的,我是一名编程初学者,所以我知道这很可怕:)}重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{return 1}重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell{let cellster=tableView.dequeueReusableCellWithIdentifier(“cellster”)cellster?.textLabel?.text=String(计数)返回cellster!}在tableView中显示数据超出了问题的范围。请查看stackoverflow以获取有关如何执行此操作的示例。