在 TableView 中删除行并移除核心数据 - iOS / Swift
Delete row in TableView and remove the core data - iOS / Swift
我的 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
}
}
我找不到你的具体问题,但是。这是一些对我有用的代码。让我知道它是否适合你。
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")
}
}
}
已解决
我的删除问题不是问题所在。 (注意:删除函数中不需要 "tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:.Fade)" 行。)
我最初遇到的问题是幻灯片可以正常工作,但系统不会提示用户删除。
确保检查 tableView 的约束。还要确保取消选中 "Constraint to margins" 选项。
我的 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
}
}
我找不到你的具体问题,但是。这是一些对我有用的代码。让我知道它是否适合你。
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")
}
}
}
已解决
我的删除问题不是问题所在。 (注意:删除函数中不需要 "tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:.Fade)" 行。)
我最初遇到的问题是幻灯片可以正常工作,但系统不会提示用户删除。
确保检查 tableView 的约束。还要确保取消选中 "Constraint to margins" 选项。