tableView 中最常见的数组元素

Posted

技术标签:

【中文标题】tableView 中最常见的数组元素【英文标题】:Most common array element in tableView 【发布时间】:2016-03-19 17:38:28 【问题描述】:

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

编辑: 很抱歉提供了一个如此糟糕的问题,正如我所说,我是编程的初学者,所以我也不是最擅长解释我的情况。但这次我会尝试更好地描述我的情况。首先,我想先用最常见的元素对我的数组进行排序,然后是第二个最常见的元素,依此类推。 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!

代码中包含的变量 prioLista 是来自另一个文件的数组。通过按一个按钮,您可以将字符串添加到其中。

My tableView after sorting elements after most common

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

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

第一行:垫第二行:Godis

在表格视图中

我希望我已经足够清楚了,并且提醒一下,我只是一个正在尝试学习编码的初学者:)

【问题讨论】:

请注意,您不能在这里得到完整的答案。请描述你做了多少以及你卡在哪里。 【参考方案1】:

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

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


【讨论】:

非常感谢!我现在只有一个问题,当我尝试将数据传递到我的 tableview 时,它最终看起来像这样: ["Food": 1, "Candy": 1] 当我向其中添加 1 个糖果和 1 个食物时。这是我的全部代码,正如我所说,我是编码的初学者,所以我知道这很可怕:) override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int return 1 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell let cellster = tableView. dequeueReusableCellWithIdentifier("cellster") cellster?.textLabel?.text = String(counts) return cellster! 在表格视图中显示数据超出了您的问题范围。请查看 *** 以获取有关如何执行此操作的示例。

以上是关于tableView 中最常见的数组元素的主要内容,如果未能解决你的问题,请参考以下文章

在 tableview 单元格中显示图像

tableViewcell页码

如何找到列表中最常见的元素? [复制]

获取 Arraylist 中最常见的元素

在 python 解释中查找列表中最常见的元素? [复制]

如何获得Int数组中最常见的值? (C#)