【问题标题】:Swift TableView Delete Cell not displayingSwift TableView删除单元格不显示
【发布时间】:2014-10-15 23:02:34
【问题描述】:

当我在 Xcode 6.0.1 中快速运行时,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
    }
}

【问题讨论】:

  • Sound 是自己制作的类还是结构体?

标签: ios uitableview core-data swift


【解决方案1】:

我想你在 iPhone 上使用它。所以你的表格视图是看不见的。您需要转到情节提要将大小类设置为 W:Compact H:Regular (它会说 W:any H:any 目前)然后将表格视图放在其中并设置您的连接,然后删除按钮就在眼前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多