ios - Swift Delete 不工作/不显示在表格 View 中

标签 ios swift ios8

所以我正在使用表格 View 构建一个应用程序。问题是要删除的滑动没有出现在模拟器中。每当我尝试向左滑动它时,什么也不会弹出。这是主视图 Controller 的代码。任何帮助将不胜感激!

  import UIKit

 class ViewController: UIViewController, UITableViewDataSource,  UITableViewDelegate {

var tableView : UITableView?

@IBOutlet weak var table: UITableView!
// Table View where the goals are displayed



override func viewDidLoad() {
    super.viewDidLoad()
navigationItem.title =  "Goal List"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {

    table.reloadData()



}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return goalMgr.goals.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell : UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

    //Assinging the contents of our goal array into rows
    cell.textLabel?.text = goalMgr.goals[indexPath.row].goal
    cell.detailTextLabel?.text = goalMgr.goals[indexPath.row].time
    return cell


}

func tableView(tableView: UITableView,
    didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var detail:SecondVC = self.storyboard?.instantiateViewControllerWithIdentifier("SecondVC") as SecondVC

    detail.cellGoal = goalMgr.goals[indexPath.row].goal
    detail.cellTime = goalMgr.goals[indexPath.row].time

    self.navigationController?.pushViewController(detail, 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){

        goalMgr.removeGoal(indexPath.row)
        table.reloadData()

        }

  }

}

这是创建 removeGoal 函数的文件

import UIKit

var goalMgr:GoalManager = GoalManager()

struct Goal{
var goal: String = "Goal"
var time: String = "Time"
 }

class GoalManager: NSObject {

var goals = [Goal]()
var persistentHelper: PersistentHelper = PersistentHelper()

override init(){

    var tempGoals:NSArray = persistentHelper.list("Goal")
    for res:AnyObject in tempGoals{
        goals.append(Goal(goal:res.valueForKey("goal")as String,time:res.valueForKey("time") as String))
    }

}

func addGoal(goal:String, time: String){

    var dicGoal: Dictionary<String, String> = Dictionary<String,String>()
    dicGoal["goal"] = goal
    dicGoal["time"] = time

    if(persistentHelper.save("Goal", parameters: dicGoal)){
        goals.append(Goal(goal: goal, time: time))
    }
}


func removeGoal(index:Int){

    var value:String = goals[index].goal

    if(persistentHelper.remove("Goal", key: "goal", value: value)){
        goals.removeAtIndex(index)
    }
}    

  }

最佳答案

您的 tableView:commitEditingStyle:forRowAtIndexPath 方法似乎已关闭。看起来您已将下面的方法粘贴到其中:

func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool
{
    return true
}

您已经在 tableView:commitEditingStyle:forRowAtIndexPath 方法之上定义了 tableView:canEditRowAtIndexPath 方法。

更新

您说滑动时不显示删除按钮。但是单元格的内容是不是向左滑动了?如果是这样,试试这个:

  1. 在模拟器中旋转您的设备(向左或向右)
  2. 确保您可以看到行的两边(分隔符应该在 View 中完全可见)
  3. 现在,滑动

您的代码中的一切似乎都很好,所以我猜删除按钮就在那里,但它在纵向模式下根本看不到。不过,它应该在横向上可见,这就是我建议执行上述步骤的原因。

如果您在横向模式下看到删除按钮,则需要使用自动布局调整 UITableView 的对齐方式。

关于ios - Swift Delete 不工作/不显示在表格 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29134638/

相关文章:

ios - CKQuery 使用 NSPredicate 搜索 NIL 总是抛出 Invalid Expression 错误

ios - 按下一个按钮后重置所有按钮

ios - 删除 iOS 应用设置

Swift-设置uitextview文本压缩文本大小

ios - 禁用特定 UIView iOS 12 的自动旋转

iOS8:NSURLSession:NSURLSessionDataTask 'Client closed connection before receiving entire response'

ios - uitableview 中的多个 Segue

ios - FBSession : No AppID provided; either pass an AppID to init, 或添加字符串值键

ios - 在 iOS 中使用 Size Classes 缩放 NSAttributedString

arrays - 从 firebase 中的数组中检索数据