I have largely populated tableView running in my project. I am trying to shuffle the tableView array at once only.I found the similar code from this link How do I shuffle an array in Swift? code works ok..But the code shuffles TableView array every time when i swipe(up/down). I just want to restrict the code to happen once only...
I am using the below code.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var indexArray = Array(MY ARRAY NAME.indices)
var index = indexArray.endIndex
let indexGenerator: AnyGenerator<Int> = anyGenerator {
if index == indexArray.startIndex { return nil }
index = index.advancedBy(-1, limit: indexArray.startIndex)
let randomIndex = Int(arc4random_uniform(UInt32(index)))
if randomIndex != index {
swap(&indexArray[randomIndex], &indexArray[index])
}
return indexArray[index]
}
let permutationGenerator = PermutationGenerator(elements: MY ARRAY NAME, indices: AnySequence(indexGenerator))
let newArray = Array(permutationGenerator)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
////////////////////////////////////
cell.textLabel?.text = newArray[indexPath.row] //use of unsolved identifier "newArray"
////////////////////////////////////
return cell
}
Thanks in Advance.