简体   繁体   English

Swift TableView删除单元格未显示

[英]Swift TableView Delete Cell not displaying

When I run this swift in Xcode 6.0.1 the text in the tableView shifts to the left as I try to swipe-to-delete. 当我在Xcode 6.0.1中运行此swift时,当我尝试滑动删除操作时,tableView中的文本会向左移动。 However, the delete button does not appear and when I release the gesture the text slides back into place and nothing is deleted. 但是,删除按钮没有出现,当我释放手势时,文本会滑回原位,并且不会删除任何内容。 Thanks for any help! 谢谢你的帮助!

import UIKit
import AVFoundation
import CoreData

class SoundListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var audioPlayer = AVAudioPlayer()

    var sounds: [Sound] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Move on...

        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // Roll Tide...

        var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!

        var request = NSFetchRequest(entityName: "Sound")

        self.sounds = context.executeFetchRequest(request, error: nil)! as [Sound]
    }

    func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        // Return the number of sections.
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let CellId: String = "cell"
        var sound = self.sounds[indexPath.row]
        var cell:UITableViewCell = self.tableView?.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell

        cell.textLabel!.text = sound.name
        //cell.textLabel!.text = sounds[indexPath.row] as? String
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var sound = self.sounds[indexPath.row]

        var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String

        var pathComponents = [baseString, sound.url]

        var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)

        self.audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL , error: nil)
        self.audioPlayer.play()

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        if editingStyle == .Delete {

            // Delete the row from the data source
            sounds.removeAtIndex(indexPath.row)
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var nextViewController = segue.destinationViewController as NewSoundViewController
        nextViewController.previousViewController = self
    }
}

I imagine your using it for iPhone. 我想您将其用于iPhone。 So you table view is out of view. 因此,您的表视图不可见。 You need to go to the storyboard set the size class to W:Compact H:Regular (it will say W:any H:any at the moment) then put the table view down in that and set up your connections the delete button will then be in view. 您需要转到情节提要中,将大小类设置为W:Compact H:Regular(此时将显示W:any H:any),然后在其中放置表格视图并设置连接,然后单击删除按钮在视野中。

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

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