删除TableView中的行并删除核心数据 - iOS / Swift

时间:2015-06-17 21:19:49

标签: swift core-data xcode6 delete-row

我的swift应用程序存在问题。我正在尝试实现滑动功能,该功能将提示用户使用" DELETE"选项。截至目前,我的代码只允许我滑动但是没有生成删除按钮。我目前也在使用Xcode 6.3.2。

提前谢谢

import UIKit // apples code for all UI related code
import AVFoundation // apple's code for audio and video
import CoreData


class SoundListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


@IBOutlet weak var tableView: UITableView!

var audioPlayer = AVAudioPlayer() // creating an audio player property as the variable audioplayer

var sounds : [Sound] = [] // creating an array to hold all sounds, in swift you need to tell the array what it will be holding, this is done by ": [Sound]" this tells the array that it will contain Sound objects


override func viewDidLoad() {
    super.viewDidLoad()

    // comment
    self.tableView.dataSource = self // added to make the Table view work
    self.tableView.delegate = self // added to make the Table view work


}

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

    var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
    var request = NSFetchRequest(entityName: "Sound") // allows to go get all the objects stored in core data
    self.sounds = context.executeFetchRequest(request, error: nil)! as! [Sound]
    self.tableView.reloadData()


}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.sounds.count // # of rows we want the table view to have based on the contents of sounds array
}

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

    var sound = self.sounds[indexPath.row] // searches the index path of each row from the array of sounds and stores to the varibale sounds once it has the correct object
    var cell = UITableViewCell() // each cell on the table
    cell.textLabel!.text = sound.name // adds the correct name from each object to the correct row

    if(indexPath.row % 2 == 0){ // adding color to each cell
        cell.backgroundColor = UIColor.lightGrayColor()
        cell.textLabel?.textColor = UIColor.whiteColor()
    }else if (indexPath.row % 2 != 0){
        cell.backgroundColor = UIColor.whiteColor()
    }

    return cell // printing the new data into each 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) // play command while asking for where the audio lives

    self.audioPlayer.play() // play audio command on click

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete){
        sounds.removeAtIndex(indexPath.row) // array of objects that we are selecting
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:.Fade)
        self.tableView.reloadData()// updated table view

    }
}

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var nextViewController = segue.destinationViewController as! newSoundViewContoller
    nextViewController.soundListViewController = self

}
}

2 个答案:

答案 0 :(得分:0)

我无法专门找到你的问题但是。这是一些适合我的代码。让我知道它是否适合你。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.dataSource = self
    self.tableView.delegate = self
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    cell.textLabel!.text = "Delete me!"
    return cell
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete){
        println("DELETED")
    }
}
}

答案 1 :(得分:0)

<强>解决

我的删除问题不是问题。 (注意:您的删除功能中不需要“tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:.Fade)”行。)

我最初遇到的问题是幻灯片可以正常工作,但用户不会提示删除。

确保使用tableView检查约束。另外,请确保取消选中“约束边距”选项。

enter image description here