【发布时间】:2018-04-28 18:11:29
【问题描述】:
我有一个带有 tableView 的 VC,在其中我通过从 tableView 的行中多次选择项目来手动填充数组。在这个 VC 中我有一个数组
var list : [QCategoryy] = [QCategoryy]()
list = NearbyPlaces.getCategories()
getCategories() 在哪里
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "bar", image: UIImage(named: "bar_button.png")!), QCategoryy(name :"night_club", image: UIImage(named: "nightclub_button.png")!), QCategoryy(name: "movie_theater", image: UIImage(named: "cinema_button.png")!), QCategoryy(name: "restaurant", image: UIImage(named: "restaurant_button.png")!), QCategoryy(name: "gym", image: UIImage(named: "gym_button.png")!), QCategoryy(name: "spa", image: UIImage(named: "spa_button.png")!), QCategoryy(name: "museum", image: UIImage(named: "museum_button.png")!)]
return list
}
这些项目(酒吧、健身房、水疗中心等)将填充我的 tableView 单元格,当我选择它们时,我将创建我的数组 selectedCategories 你如何在这里看到:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == nearbySearchSegueIdentifier {
let selectedCategories: [QCategoryy] = tableView.indexPathsForSelectedRows?.map({ (indexPath) -> QCategoryy in
return list[indexPath.row] }) ?? []
if let selectedRows = tableView.indexPathsForSelectedRows {
if let vc : nextClass = segue.destination as? nextClass {
vc.categories = selectedCategories
}
}
}
}
现在我想知道如何在不选择 tableView 的单元格的情况下填充这个数组 (selectedCategories) 而是通过按下按钮以随机方式填充该数组,所以我想用我的 @987654327 的随机项填充数组@点击按钮,我该怎么做?
更新
struct QCategoryy {
var name: String
var image: UIImage
var isSelected = false
init(name:String, image:UIImage) {
self.name = name
self.image = image
}
}
extension QCategoryy: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.name = value
self.image = UIImage()
}
init(unicodeScalarLiteral value: String) {
self.init(name: value, image: UIImage())
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(name: value, image: UIImage())
}
}
【问题讨论】:
-
重复有没有关系?
-
如果我没有重复会更好
标签: ios arrays swift uitableview swift3