Thor Larson
Thor Larson

Reputation: 43

How to get the tableViewCell not to expand when swipe to delete

I have my app set up so that I can add a cell to a ViewController where the cell appears in the tableView but when you click on the cell it expands to show more information. Here is the code...

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var selectedIndexPath: NSIndexPath? = nil

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.registerNib(UINib(nibName: "CustomViewCell", bundle: nil), forCellReuseIdentifier: "Cell")

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomViewCell

    cell.clipsToBounds = true

    return cell

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if selectedIndexPath != nil {

        if indexPath == selectedIndexPath {
            return 140
        } else {
            return 38
        }

    } else {
        return 38
    }

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     // New
    switch selectedIndexPath {
    case nil:
        selectedIndexPath = indexPath
    default:
        if selectedIndexPath! == indexPath {
            selectedIndexPath = nil
        } else {
            selectedIndexPath = indexPath
        }
    }

    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

}

Everything works fine with the expanding and closing for the cell except that I configured the cell to swipe to delete but the cell just expands when I swipe to delete. Heres the code:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {


    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete") { (action , indexPath ) -> Void in
        self.editing = false     
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let moc = appDelegate.managedObjectContext
        moc.deleteObject(self.events[indexPath.row])
        appDelegate.saveContext()             
        self.events.removeAtIndex(indexPath.row)
        tableView.reloadData()

    }

    return [deleteAction]

}

Before swipe to delete

After swipe to delete

I dont want the the cell to expand like it is when I swipe to delete, Any ideas?

Upvotes: 0

Views: 209

Answers (1)

matt
matt

Reputation: 534950

I think you may be the one who is expanding the cell. You are saying:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath == selectedIndexPath {
        return 140
    }
}

Is that method being called during swipe to delete? Use debugging to find out. If it is, you'll need to rewrite that code so that, just in case you're in the middle of swipe to delete, you don't return 140.

Upvotes: 1

Related Questions