关于uitableview:如何从数组中获取一个随机元素而不是多次获取相同的元素?

How to grab a random element from an array without grabbing the same element more than once?

本问题已经有最佳答案,请猛点这里访问。

当我使用arc4random_uniform()方法从数组中获取元素时,通常会多次获取数组中的相同元素。我正在尝试使数组中的每个元素只被捕获一次。我之所以要这样做,是因为一个UITableView中有不止一个细胞没有相同的文本。这是UITableView中单元格文本的数组:

1
var definitions = ["Used to carry the pharoah","Used to carry bodies as a ceremony","Had a flat deck to carry a farmer's treasure","Daily, it made a trip around the world to carry Ra","Towed by smaller boats, carrying heavy objects","Used for business and pleasure by officials/nobles","Carried most Egyptians and some goods"]

在我的viewDidLoad()方法中,我这样做是为了调用definitions的随机元素:

1
self.boats = [Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))])]

如何修改代码,使两个元素不会在我的UITableView中显示两次?谢谢您!


保留一个随机索引值的列表,如果随后的随机索引在该列表中,则跳过该列表并生成另一个随机索引。