Fahim Parkar
Fahim Parkar

Reputation: 31633

swipe not showing delete button

It's really odd. :(

I am trying to implement swipe to delete in tableview. For this below is what I have.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"commitEditingStyle===%@", editingStyle);
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        NSLog(@"now delete this cell");
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

Still when I swipe, swipe is done, but I can't see Delete button.

enter image description here

Any idea what is going on?


Edit 1

Something more weird now.

When I say mainTableView.editing = YES; in viewDidLoad, I have below.

enter image description here

Why delete option is appearing on the left side?

Also with editing option, still it appear same as first image.


Edit 2

// table view delegates
- (int)numberOfSectionsInTableView:(UITableView *) tableView {
    return 1;
}

- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
    return actualProductsArray.count;
}

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];

    // created label and added into cell...

    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;

}

Upvotes: 2

Views: 2405

Answers (6)

One In a Million Apps
One In a Million Apps

Reputation: 361

I had the same problem and it wasn't related to the width of the table or not having canEditRowAtIndexPath. I noticed the right nav bar button (Edit) would flash when I swiped left. The bug I found in my code was that I was calling tableView.reloadData() inside the setEditing method.

override func setEditing(editing: Bool, animated: Bool) {
     super.setEditing(editing, animated: animated)
    tableView.reloadData()
}

When I deleted the tableView.reloadData() line, the Delete button showed up just as expected. The reason I was doing the reloadData was because in Edit mode, I added an Insert line at the bottom of the table with an editingStyleForRowAtIndexPath = .Insert

Upvotes: 1

r_19
r_19

Reputation: 1948

I know this has been solved for over a year now but for some people that might encounter this problem, This might be just a constraint issue.That is how I solved mine.

Can't believe I've been trying to solve this problem for days, literally!, because I thought it was somewhere in my code implementation because I transitioned from UITableViewController to UITableView on a UIViewController. I just copied the UITableViewCell from the original UITableViewController and the code works well from the original UITableViewController implementation.

@Fahim Parkar first screenshot is pretty much the same scenario as mine. Slide and not showing delete. I think this means that canEditRowAtIndexPath is already implemented. His answer I believed is set in code but it led me to try out to check my constraints and finally fixed it

Upvotes: 0

Fahim Parkar
Fahim Parkar

Reputation: 31633

Though this is not real answer, but below was error.

While defining size of tableview, I had defined its width as 1080 against 320 and hence I was not able to see delete button as it was way ahead of screen.

Upvotes: 4

Mert
Mert

Reputation: 6065

I think you add the label directly on cell and not on cell.contentView.

If you provide the code here

// created label and added into cell...

We can help you more.

tableView.editing = true 

has the behaviour like you have on image Edit 1. (Buttons are on left side) But the buttons are on content, which should not. Because you are adding labels on cell directly

please add the labels in contentView and try again

Upvotes: 0

Manthan
Manthan

Reputation: 3914

I am only using the below line for swipe to delete in my code it works perfectly fine for me.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source
        //[self.tblinboxmsg deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

Hope it helps you too...

Upvotes: 0

Developer
Developer

Reputation: 760

You have to try Tableview.editing=YES in ViewDidload

Upvotes: 0

Related Questions