Итак, я создаю приложение, используя вид таблицы. Проблема заключается в том, что удаленный доступ к удалению не отображается в симуляторе. Когда я пытаюсь пронести его влево, ничего не всплывает. Вот код для главного контроллера. Любая помощь будет оценена!Swift Удалить не работает/отображается в виде таблицы
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)
}
}
}
Я также использую основные данные. – Amir
Является ли ваш источник данных tableView и делегат установленным для себя? – Aaron
Что это значит? – Amir