So I am building an app using a table view. The problem is that the swipe to delete is not showing up in the simulator. When ever I try to swipe it to the left, nothing pops up. Here is the code for the main view controller. Any help would be appreciated!
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()
}
}
}
Here is the file in which the removeGoal function is created
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)
}
}
}
Your tableView:commitEditingStyle:forRowAtIndexPath
method seems off. It looks like you've pasted the method below inside of it:
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool
{
return true
}
You've already defined the tableView:canEditRowAtIndexPath
method above the tableView:commitEditingStyle:forRowAtIndexPath
method.
Update
You say that when you swipe, the delete button isn't shown. But is the content of the cell swiped to the left? If so, try this:
Everything seems fine in your code, so I'm guessing that the delete button is there, but it is simply out of view in portrait mode. It should be visible in landscape though, that's why I'm suggesting the above steps.
If you see the delete button in landscape mode, you need to adjust your alignment of your UITableView with auto layout.
To get swipe to work you have to implement tableView:editingStyleForRowAtIndexPath:
In this function you return the editing style your row supports. Only if this is implemented does swipe to delete work. So in here return UITableViewCellEditingStyle.Delete
.
If you want to change the text label in the red box which shows on swipe, implement tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:
tableView:canEditRowAtIndexPath:
is only there to let you veto which rows support being edited.
Another thing to check is that "User Interaction Enabled" is checked for the Table View Cell in the Attributes Inspector. It is checked by default, but if you have unchecked it at some point the swipe to delete function will not work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.