【问题标题】:Sorting TableView Rows对 TableView 行排序
【发布时间】:2017-06-26 00:15:51
【问题描述】:

---- 已回答----

我目前正在尝试按标签上的单词对我的 tableview 进行排序。每行至少有 9 种颜色可供用户选择(用户可以选择任意数量)。关于如何组织表格,我有一个特定的顺序,但是根据用户首先选择的行,行的顺序可以是任何东西(问题)。

我在想的是我按照我希望单词在第二个屏幕中显示的顺序创建一个数组:“红色,蓝色,绿色,......”然后以某种方式将该数组连接到 tableview。因此,如果 tableview 遵循这个顺序(红色、蓝色、绿色......),并且用户确实选择了蓝色,那么表格将被排序为“红色、绿色、......”。这意味着无论用户拥有或没有什么颜色,tableview 都将遵循数组的顺序。我尝试在谷歌上搜索这个问题的解决方案好几天,但似乎无法弄清楚。有什么建议么?我已经粘贴了颜色如何加载的代码:

func loadColors() {
        let colorQuery = PFQuery(className: "Colors")
        colorQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user
        colorQuery.limit = 10
        colorQuery.findObjectsInBackground { (objects, error) in
            if error == nil {
                self.colorTypeArray.removeAll(keepingCapacity: false)
                self.colorNameArray.removeAll(keepingCapacity: false)                
                self.colorObjectIDArray.removeAll(keepingCapacity: false)

                for object in objects! {
                    self.colorTypeArray.append(object.value(forKey: "colorType") as! String) // add data to arrays
                    self.colorNameArray.append(object.value(forKey: "colorName") as! String) // add data to arrays                    
                    self.colorObjectIDArray.append(object.objectId!) //appends the objectid
                }
                self.tableView.reloadData()

            } else {
                print(error?.localizedDescription ?? String())
            }
        }
    }

//places colors in rows
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ColorsCell //connects to color cell

        cell.colorType.text = colorTypeArray[indexPath.row] //goes through array and puts colors on cell label

        return cell
    }

【问题讨论】:

    标签: swift uitableview sorting parse-platform xcode8


    【解决方案1】:

    据我了解您的问题,用户有一个可以添加更多颜色的颜色列表。您的目标是确保即使用户将任意颜色添加到列表中,它们也会保持您预定义的某种顺序。

    这是我将如何处理此问题的简化示例。基本上,正如您在问题中所说,您必须定义一个颜色数组,用作了解正确顺序的参考(下面的patternColors)。

    然后,当您的颜色列表发生变化时(例如,您从服务器检索列表,或者用户添加或删除了颜色),重新排序列表,如下所示。

    func sort(colors: [String]) -> [String] {
    
        // This is the order I want the colors to be in.
        var patternColors = ["Red", "Blue", "Green"]
    
        // We use `map` to convert the array of three colors into
        // an array of subarrays. Each subarray contains all the 
        // colors that match that particular pattern color (we use
        // `filter` to find all these matching colors.
        // Finally, we use `flatMap` to convert this array of 
        // subarrays back into a normal one-dimensional array.
        var sortedColors = patternColors.map { patternColor in
            colors.filter { color in
                color == patternColor
            }
        }.flatMap { $0 }
    
        return sortedColors
    }
    
    
    var unsortedColors = ["Blue", "Red", "Blue", "Red", "Red", "Blue", "Green", "Red"]
    
    let sortedColors = sort(colors: unsortedColors)
    print(sortedColors) // ["Red", "Red", "Red", "Red", "Blue", "Blue", "Blue", "Green"]
    

    【讨论】:

    • 我在收集数据并使用 print(sortedColors) 后立即在代码中添加了这个,数组显示为空
    • 是你的代码有问题还是我的? patternColors 的值是否适合您的情况?
    • 啊,对不起,是我的代码出了问题!谢谢!
    猜你喜欢
    • 2018-11-04
    • 2014-06-14
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多