Swift 对TableView行进行排序
----回答---- 我目前正试图根据标签上的文字对tableview进行排序。每行至少有9种颜色中的一种,用户可以选择(用户可以选择任意数量)。关于如何组织表,我心里有一个特定的顺序,但取决于用户首先选择的行,行的顺序可以是任何东西(问题) 我当时的想法是,我按照我希望在第二个屏幕上显示的单词的顺序制作一个数组:“红、蓝、绿……然后以某种方式将这个数组连接到tableview。因此,如果tableview遵循此顺序(红色、蓝色、绿色,…),并且用户选择蓝色,则该表的顺序将为“红色、绿色,…”。这意味着无论用户有或没有什么颜色,tableview都将遵循数组的顺序。我在谷歌上搜索了好几天这个问题的解决方案,但似乎无法找到答案。有什么建议吗?我已粘贴颜色加载方式的代码:Swift 对TableView行进行排序,swift,uitableview,sorting,parse-platform,xcode8,Swift,Uitableview,Sorting,Parse Platform,Xcode8,----回答---- 我目前正试图根据标签上的文字对tableview进行排序。每行至少有9种颜色中的一种,用户可以选择(用户可以选择任意数量)。关于如何组织表,我心里有一个特定的顺序,但取决于用户首先选择的行,行的顺序可以是任何东西(问题) 我当时的想法是,我按照我希望在第二个屏幕上显示的单词的顺序制作一个数组:“红、蓝、绿……然后以某种方式将这个数组连接到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
}
正如我理解你的问题,用户有一个颜色列表,他们可以添加更多的颜色。您的目标是确保即使用户将任意颜色添加到列表中,它们仍保持您预定义的某种顺序 下面是一个我将如何处理这个问题的简化示例。基本上,正如您在问题中所说,您必须定义一个颜色数组,该数组将用作了解正确顺序的参考(
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"]
正如我理解你的问题,用户有一个颜色列表,他们可以添加更多的颜色。您的目标是确保即使用户将任意颜色添加到列表中,它们仍保持您预定义的某种顺序 下面是一个我将如何处理这个问题的简化示例。基本上,正如您在问题中所说,您必须定义一个颜色数组,该数组将用作了解正确顺序的参考(
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
是否为您的案例获得了正确的值?啊,我很抱歉,是我的代码出了问题!谢谢在我收集数据并使用print(sortedColors)后,我立即在代码中添加了这一点,数组显示为空。是您的代码出错还是我的代码出错?patternColors
是否为您的案例获得了正确的值?啊,我很抱歉,是我的代码出了问题!谢谢