对 TableView 行进行排序
Sorting TableView Rows
---- 已回答----
我目前正在尝试根据标签上的字词对我的 table视图进行排序。每行至少有 9 种颜色中的一种可供用户选择(用户可以根据需要选择任意数量)。关于如何组织 table,我心中有一个特定的顺序,但取决于用户首先选择哪一行,行的顺序可以是任何东西(问题)。
我在想的是,我按照我希望单词在第二个屏幕上显示的顺序创建一个数组:"red, blue, green,...." 然后以某种方式将这个数组连接到 table 视图。因此,如果 table 视图遵循此顺序(红色、蓝色、绿色......),并且用户确实选择了蓝色,则 table 将按 "red, green,..." 排序。这意味着无论用户有或没有什么颜色,table视图都将遵循数组的顺序。我尝试用谷歌搜索这个问题的解决方案好几天了,但似乎无法弄清楚。有什么建议么?我已经粘贴了我的颜色加载代码:
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
}
据我了解你的问题,用户有一个颜色列表,他们可以向其中添加更多颜色。您的目标是确保即使用户向列表中添加任意颜色,它们也会保持您预定义的某种顺序。
这是我将如何处理这个问题的简化示例。基本上,正如您在问题中所说,您必须定义一个颜色数组,用作了解正确顺序的参考(下面的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 { [=10=] }
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"]
---- 已回答----
我目前正在尝试根据标签上的字词对我的 table视图进行排序。每行至少有 9 种颜色中的一种可供用户选择(用户可以根据需要选择任意数量)。关于如何组织 table,我心中有一个特定的顺序,但取决于用户首先选择哪一行,行的顺序可以是任何东西(问题)。
我在想的是,我按照我希望单词在第二个屏幕上显示的顺序创建一个数组:"red, blue, green,...." 然后以某种方式将这个数组连接到 table 视图。因此,如果 table 视图遵循此顺序(红色、蓝色、绿色......),并且用户确实选择了蓝色,则 table 将按 "red, green,..." 排序。这意味着无论用户有或没有什么颜色,table视图都将遵循数组的顺序。我尝试用谷歌搜索这个问题的解决方案好几天了,但似乎无法弄清楚。有什么建议么?我已经粘贴了我的颜色加载代码:
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
}
据我了解你的问题,用户有一个颜色列表,他们可以向其中添加更多颜色。您的目标是确保即使用户向列表中添加任意颜色,它们也会保持您预定义的某种顺序。
这是我将如何处理这个问题的简化示例。基本上,正如您在问题中所说,您必须定义一个颜色数组,用作了解正确顺序的参考(下面的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 { [=10=] }
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"]