我正在制作音乐播放器节目我在所有部分都收到了所有歌曲

时间:2017-07-25 06:07:43

标签: ios swift uitableview

import UIKit

var songs:[String] = []
let abcArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"]

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

    func numberOfSections(in tableView: UITableView) -> Int {
        return abcArray.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return abcArray[section]
    }

    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
         return abcArray
    }

    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) {
        //
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        gettingSongName()
    }

    func gettingSongName()
    {
        let folderURL = URL(fileURLWithPath: Bundle.main.resourcePath!)

        do
        {
            let songPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)

            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: "")
                    mySong = mySong.replacingOccurrences(of: "%", with: "")
                    mySong = mySong.replacingOccurrences(of: "190", with: "")
                    mySong = mySong.replacingOccurrences(of: "Kbps", with: "")
                    songs.append(mySong)
                }
            }

            myTableView.reloadData()
        }
        catch
        {
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您为每个部分指定了错误的numberOfRows。这样做:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
{
    switch(section) {
    case 0:
        return Section1.count
    break

    case 1:
        return Section2.count
    break

    default : 
        return SectionDefault.count
    break : 
    }
}