0

I have UITableViewController with files from document folder. I name the cells by artist name. I have three artist and four songs. UITableViewCell shows two cells with the same artist. How can I fix it?

enter image description here This code export data from document folder

var mp3Files: Array<String!>!
func exportData() {
    var generalURL: [AnyObject]?
    var arrayFiles: Array<NSURL!>!
    var directory = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    var urlFromDirectory = directory.first as! NSURL

    var file = fileManager.contentsOfDirectoryAtURL(urlFromDirectory, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil)!
    println("file \(file)")

    mp3Files = file.map(){ $0.lastPathComponent }.filter(){ $0.pathExtension == "mp3" }

  println("mp3 files  \(mp3Files)")
}

and code fill the UITableViewCell

    var cellStrings: String!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

      var dataForCell = mp3Files[indexPath.row]
    var generalURL: NSURL!

    var documentFolder = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)

    if var urlFromFolder: NSURL = documentFolder.first as? NSURL {
        generalURL = urlFromFolder.URLByAppendingPathComponent(dataForCell)
        println("general \(generalURL)")
    }

    var player = AVPlayerItem(URL: generalURL)
    var metaData = player.asset.commonMetadata as! [AVMetadataItem]
    for item in metaData {
        if item.commonKey == "artist" {
            nameArtist = item.stringValue
        }
    }

        cell.textLabel?.text = nameArtist
    //
     cellStrings = cell.textLabel?.text
    println("cell strings \(cellStrings)")
    // Configure the cell...

    return cell
}
Alexander Khitev
  • 6,417
  • 13
  • 59
  • 115
  • Are you sure that the media file is not duplicated in the documents folder? You can check this very easily by [enabling file sharing](http://stackoverflow.com/questions/6029916/how-to-enable-file-sharing-for-my-app) and then examine the contents of the documents folder from iTunes – Kumuluzz Jul 29 '15 at 13:50
  • Files are not duplicated, because it is a variety of songs but one performer. – Alexander Khitev Jul 29 '15 at 13:57
  • What is logged by `println("file \(file)")` and `println("mp3 files \(mp3Files)")` in your method called `exportData`? – Kumuluzz Jul 29 '15 at 14:04

1 Answers1

0
 var superArray = [String]()
var filterArray = [String]()
func filter() {
    var proString: String!
    for proItem in mp3Files {
        var proFolder = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
        var americaURL: NSURL!
        if var proURL: NSURL = proFolder.first as? NSURL {
            americaURL = proURL.URLByAppendingPathComponent(proItem)
        }
        var proPlayerItem = AVPlayerItem(URL: americaURL)
        var proData = proPlayerItem.asset.commonMetadata as! [AVMetadataItem]
        for proFiles in proData {
            if proFiles.commonKey == "artist" {
                superArray.append(proFiles.stringValue)
            }
        }
    }
    filterArray = Array(Set(superArray))
    filterArray.sort(){ $0 < $1 }
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1 ?? 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return filterArray.count ?? 0
}

var name: String!
var nameArtist: String!


//
var cellStrings: String!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    nameArtist = filterArray[indexPath.row]

    cell.textLabel?.text = nameArtist

    return cell
}
Alexander Khitev
  • 6,417
  • 13
  • 59
  • 115