当我在Xcode 6.0.1中运行此swift时,当我尝试滑动到删除时,tableView中的文本向左移动。但是,删除按钮不会出现,当我释放手势时,文本会滑回原位并且不会删除任何内容。谢谢你的帮助!
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
}
}
答案 0 :(得分:4)
我想你在iPhone上使用它。所以你的表视图是不可见的。您需要将故事板设置为W:Compact H:Regular(它会说W:任何H:任何时刻)然后将表格视图放在那里并设置连接然后删除按钮在视野中。