ios - Swift TableView 删除单元格不显示

标签 ios uitableview core-data swift

当我在 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
    }
}

最佳答案

我想您将它用于 iPhone。所以你的 TableView 不在 View 中。您需要转到 Storyboard ,将尺寸等级设置为 W:Compact H:Regular(此时会显示 W:any H:any),然后将表格 View 放在其中并设置您的连接,然后删除按钮将可见。

关于ios - Swift TableView 删除单元格不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393555/

相关文章:

ios - 在 Swift 中使用 UITableView 以编程方式切换 View

ios - 如何将 View 从纵向模式更改为横向模式并锁定它?

iphone - 沿曲线滚动

ios - '-[UITableViewController loadView] 加载了 "OG7-5c-Gm7-view-xGh-Gb-9hr"nib 但没有获得 UITableView。

swift - 本地存储数据的最佳方式是什么(IOS - xcode)

iphone - 我应该注意什么来调试这个 Core-Data/NSFetchedResultsController 错误?

ios - 如何使用 GCD 进行资源的轻量级事务锁定?

ios - Digits - 匹配来自非 Digits 用户的联系人

ios - UITableView - 标题高度更改后重新定位单元格

ios - 设置两个新创建的Core Data对象的关系