现在有2天多的时间,我正按照以下教程和指南尝试按名称对表格视图进行排序,但没有答案。我只发现了在数组中具有已创建值的教程,但事实并非如此。它应该按字母顺序从A到Z排序。我也尝试使用一些教程,但是他们对sortedBy使用了一种令人困惑的方法。有人可以帮我吗?
import UIKit
import AVFoundation
var audioPlayer = AVAudioPlayer()
var songs:[String] = []
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: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = songs[indexPath.row]
cell.textLabel?.textColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
cell.backgroundColor = UIColor(red:0.15, green:0.15, blue:0.34, alpha:1.0)
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")
}
}
override func viewDidLoad()
{
super.viewDidLoad()
gettingSongNames()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
gettingSongNames()
}
//FUNCTION THAT GETS THE NAME OF THE SONGS
func gettingSongNames()
{
let folderURL = URL(fileURLWithPath:Bundle.main.resourcePath!)
do
{
let songPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
//loop through the found urls
for song in songPath
{
var mySong = song.absoluteString
if mySong.contains(".mp3")
{
let findString = mySong.components(separatedBy: "/")
mySong = findString[findString.count-1]
mySong = mySong.replacingOccurrences(of: "%20", with: " ")
mySong = mySong.replacingOccurrences(of: ".mp3", with: "")
songs.append(mySong)
}
}
myTableView.reloadData()
}
catch
{
print ("ERROR")
}
}
}