繁体   English   中英

Swift iOS中的tableView

[英]tableView in Swift iOS

我需要以下代码的帮助。 由于某种原因,歌曲列表没有显示在表格中。 从我的角度来看一切都很好。 如果有人看到明显的东西,请指出。 这是我的快速代码:

    func getMusicFilesInDirectory() -> [String] {

    // get the documents folder url
    let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

    // get the directory contents (including folders)
    do {
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
        print(directoryContents)

    } catch let error as NSError {
        print(error.localizedDescription)
    }
    // filter the directory to extract only Wav Files

    do {
        let directoryUrls = try  NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
        print(directoryUrls)
        let wavFilesDir = directoryUrls.filter(){ $0.pathExtension! == "wav" }.map{ $0.lastPathComponent! }
        wavFiles = ["Music Files:\n" + wavFilesDir.description]
       } catch let error as NSError {
        print(error.localizedDescription)
    }
     return wavFiles
                                       }
// table view
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return wavFiles.count;
    return getMusicFilesInDirectory().count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel?.text = getMusicFilesInDirectory()[indexPath.row]
    cell.textLabel?.text = wavFiles[indexPath.row]
   return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("You selected cell#\(indexPath.row)")}

在遵循了下面这些家伙的一些建议之后,我修改了代码并添加了以下方法。 当前,我仍然无法在列表中显示该列表,原因是因为无法在存储的目录中访问音乐文件。 一旦将它们移到文档目录,就应该解决所有问题。 完成后,我将发布另一个更新。

    func tableView(tableView: UITableView, didSelectRowAtIndexPath   indexPath: NSIndexPath) {
    getFileCount({(success: Bool, error: String)-> Void in
        if success{
            tableView.reloadData();
        } else {
            print("No wave files Found");
        }
        })
    //print("You selected cell#\(indexPath.row)")
       }

func getFileCount (completionHandler : (success: Bool, error: String) -> Void) -> [String] {
    if wavFiles.count > 0 {
        completionHandler(success: true, error: "none");
    }else {
        completionHandler(success: false, error: "No files found");
    }
    return wavFiles
    }
func sampleFunction (completionHandler : (success : Bool, error : String) -> Void) -> [String] {
    var someVariable = ["value1","value2","value2"];
    if someVariable.count > 0 {
        completionHandler(success: true, error: "");
    } else {
        completionHandler(success: false, error: "Some Problem");
    }
    return someVariable;
}

var dataHolder = sampleFunction({(success: Bool, error : String)-> Void in
    if success{
        tableView.reloadData();
    } else {
        print("Some Error");
    }
})

这是您可以尝试实现的示例代码。
我建议使用完成处理程序,因为您是从某个外部来源或类似来源获取所有值的,因此,当检索部分在后台工作时,将完成执行过程。
因此,不必每次都使用这些处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM