-2

I am trying to randomly populate a UITableView with the contents of my directory.

import UIKit
import AVFoundation

var songs:[String] = []
var audioPlayer = AVAudioPlayer()
var thisSong = 0
var audioStuffed = false

class FirstViewController: UIViewController, UITableViewDelegate,    UITableViewDataSource {
    @IBOutlet weak var myTableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection  section: Int) -> Int {
        return songs.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = songs[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        do
        {
            let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3")
            try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
            audioPlayer.play()
            thisSong = indexPath.row
            audioStuffed = true
        }
        catch
        {
            print ("ERROR")
        }
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • And what's the problem? Please read https://stackoverflow.com/help/how-to-ask and https://stackoverflow.com/help/mcve. If you have an error, state the exact error. If you have unexpected behaviour, state exactly how what you get differs from what you expect. click `edit` above to add information that will help us help you. – SherylHohman Feb 01 '18 at 06:37
  • Thank you Sharyl, sorry, I did not know how-to-ask. The problem I was having was figuring out random strings in UITables. The problem was not with the code, it was with me, lol. – James Belk Feb 01 '18 at 11:02
  • No Problem. All the best. – SherylHohman Feb 01 '18 at 22:06

2 Answers2

1

Do you want to shuffle your songs array? You can using this (https://github.com/SwifterSwift/SwifterSwift/blob/master/Sources/Extensions/SwiftStdlib/ArrayExtensions.swift)

public mutating func shuffle() {
    // http://stackoverflow.com/questions/37843647/shuffle-array-swift-3
    guard count > 1 else { return }
    for index in startIndex..<endIndex - 1 {
        let randomIndex = Int(arc4random_uniform(UInt32(endIndex - index))) + index
        if index != randomIndex { swapAt(index, randomIndex) }
    }
}
Quoc Nguyen
  • 2,839
  • 6
  • 23
  • 28
  • Thank you Quoc Nguyen for taking the time to help me. I appreciate it very much. Thanks for the link as well. – James Belk Feb 01 '18 at 09:27
0

First you need to random array and you can random array using -

override func viewDidLoad() {
   super.viewDidLoad()
   songs.shuffle()
}

extension Array {
    mutating func shuffle() {
        for _ in 0..<self.count {
            sort { (_,_) in arc4random() < arc4random() }
        }
    }
}
Kamani Jasmin
  • 691
  • 8
  • 11
  • Awesome, thank you very much. I greatly appreciate the help. By adding the extension array and then adding songs.shuffle() to the viewDidload, it worked beautifully. Exactly what I wanted. Thanks. – James Belk Feb 01 '18 at 09:23