0

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.

Community
  • 1
  • 1
Joe
  • 8,868
  • 8
  • 37
  • 59
  • When are you calling this code block? Try just calling this block in viewDidLoad – Zachary Espiritu Mar 19 '16 at 14:06
  • @Zachary Espíritu i moved the code to ViewDidLoad..but the code not calling my newArray. i am getting the unsolved identifier error...i updated the code. please take look.thanks – Joe Mar 19 '16 at 14:30

1 Answers1

0

Just call this inside of your viewDidLoad and it should shuffle your table view cells only once. There's no need to call it in every cell.

Zachary Espiritu
  • 937
  • 7
  • 23