gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:17:57 24 4
gpt4 key购买 nike

我的 swift 应用程序遇到问题。我正在尝试实现滑动功能,该功能将提示用户使用“删除”选项。截至目前,我的代码只允许我滑动,但不会生成删除按钮。我目前也在使用 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")
}
}
}

关于swift - 删除 TableView 中的行并删除核心数据 - iOS/Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30902580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com