I have a VC with a tableView inside where i fill manually an array by multiple selecting of items from my tableView's rows. In this VC i have an array
var list : [QCategoryy] = [QCategoryy]()
list = NearbyPlaces.getCategories()
where getCategories()
is
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
}
these items (bar, gym, spa etc.) will fill my tableView cells and when i select them i'll create my array selectedCategories
how you can see here:
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
}
}
}
}
now i would like to know how can i fill this array (selectedCategories
) without selecting the cells of the tableView but in random way by pressing a button, so i want to fill the array with random items of my var list : [QCategoryy] = [QCategoryy]()
with a button tap, how can i do?
UPDATE
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())
}
}