Is it possible to programmatically show the red delete button on a UITableViewCell?
Asked Answered
H

4

26

There are lots of similar questions on here, but none that I think specifically ask this question, which is, is there any way in code to force the red delete button to appear at the right-side of a UITableView row?

The reason I ask is that I'm trying to change the behaviour of a tableview using two UISwipeGestureRecognizers such that:

  • a single-finger swipe invokes a custom action (instead of causing the red delete button to show, which is how it behaves now), and

  • a double-finger swipe invokes the default single-finger swipe behaviour, i.e. causes the red delete button to show.

I have scoured through the SDK docs but I can't find any way of causing that red button to appear, which makes me think that the proposed UI scheme above is impossible to implement without manually creating the red delete button from scratch and trying to make it emulate the built-in one's behaviour.

Any help is much appreciated.

Hubble answered 14/3, 2011 at 16:53 Comment(6)
Sorry if I'm missing the point, but doesn't this do the trick? [cell setEditing:YES animated:YES];Then
That was the first thing I tried. What that does is the same thing that happens when you tap the UITableViewController's editButtonItem, i.e. it reveals the editing accessory for that cell, which in most cases is the little blue detail disclosure indicator, not the red delete button. Since there is no such thing as UITableViewCellAccessoryDeleteButton to parallel UITableViewCellAccessoryDetailDisclosureButton, invoking setEditing: on a cell doesn't help. There is a [UITableCellView showingDeleteButton] but its a readonly property :(Hubble
Have you found a solution for this? I have the exact same problem (well my mechanism to delete is not 2-finger swipe, but just a button).Thalassic
Not yet - according to the iOS 5 SDK docs the UITableViewCell showingDeleteConfirmation property is still read-only, and even though UITableViewCellStateShowingDeleteConfirmationMask is exposed and can be reacted to in the state transition callbacks, I can't see a way to manually trigger those state transitions such that the red delete button will appear.Hubble
Might we try, after invoking setEditing:YES animated:NO, programatically hitting the red button and then hiding it? I would expect the delete button to appear this way. Of course, we would have to prevent or undo the indentation caused by the red button appearing.Alacrity
Is there any update on this? I would like for my accessoryButton to be a cross, of which tapping results in showing the red delete button on the right side.Keven
S
4

In the doubleFingerSwipe gesture's selector method set one variable and assign as no of row swiped and reload the table . In the

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == yourVariable) return UITableViewCellEditingStyleDelete;
    else return UITableViewCellEditingStyleNone;
}

I think thats work for ur problem.

Sheryl answered 18/5, 2012 at 10:44 Comment(0)
Z
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if(TableMethod==1)
        {
            UIButton *delete_btn=[UIButton buttonWithType:UIButtonTypeCustom];
            [delete_btn setFrame:CGRectMake(10,15,25,25)];
            delete_btn.tag=indexPath.row;
            [delete_btn setImage:[UIImage imageNamed:@"remove_Icone.png"] forState:UIControlStateNormal];
            [delete_btn addTarget:self action:@selector(delete_btn:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:delete_btn];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Configure the cell.
    return cell;
}


- (IBAction)delete_btn:(id)sender
{
    UIButton *buttontag = (UIButton *)sender;
    //NSLog(@"%d",buttontag.tag);
    Delete_row = buttontag.tag;

    UIAlertView *Alert = [[UIAlertView alloc]initWithTitle:@"Delete Reservation"   message:@"Are you sure to want Delete Reservation ??" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel",nil];
    Alert.tag=1;
    [Alert show];
    [Alert release];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(alertView.tag==1)
    {
        if(buttonIndex == 0)
        {
            // delete row
        }
    }
}
Zeiler answered 3/9, 2013 at 7:31 Comment(3)
could you walk through what is happening in the Tablemethod == 1 block? It appears to me that you are creating the custom button but I'm not sure. @BhaveshDoublecross
what ? i can't understand your problem @Unome.Zeiler
It wasn't a problem, I was curious for some explanation on your code. Where does TableMethod get defined? Is it a property on the table view? I do table views in swift not objective c so I don't know if its something language specific.Doublecross
C
-1

This is not an answer to your question, but a suggestion about the functionality (I can't leave comments yet, but I would still like to offer my two cents).

From a UI/UX perspective, overriding the default or "expected" functionality is not a great idea. Users have certain expectations about how various apps and features will work, and when they don't work in that manner, the users either get frustrated or confused. Hence the proliferation of the pull-to-refresh functionality in nearly every app that involves a table view. What would make more sense would be to have the two-finger swipe be the custom functionality. Not only would your app then conform to traditional UX guidelines (in fact, I think Apple might reject your app as it stands, and that is probably why you're having such a hard time finding an answer), but it would involve a lot less struggle/custom code.

So sorry that wasn't a direct answer to your question, but I hope it helps.

Cockatrice answered 27/2, 2012 at 18:50 Comment(1)
I agree in principle, although I would say two things about this: (1) there are a huge number of users who are unaware of the swipe-to-delete UI convention on iOS and (2) it is possible for the usability gain (in terms of reduced manual effort) to complete a task that is performed with high frequency to exceed the "confusion loss" of breaking a UI convention, esp. when that UI convention is not widely known as per (a). But in strict principle, I do agree with you :)Hubble
C
-2

By calling

[self.tableView setEditing:YES animated:YES];

on a UITableViewController you can set the tableView to editing mode. you can actually add a gesture recognizer to the cell a cell view that makes that call using a delegate method.

According to the Documentation of UITableViewCell you can actually set the same way an individual cell. Apart from that you will have to manage the standard gesture recognizer. I guess subclassing would be your ally on that situation.

I hope this helps.

Caster answered 14/3, 2011 at 18:57 Comment(2)
Unfortunately, setEditing: on the whole table just invokes setEditing: on each cell for which editing is allowed, and as per my comment on NWCoder's answer, this doesn't show the delete button. Thanks for the response anyway.Hubble
Regardless of whether this is the right answer for this question or not, i believe this is the best general, well-rounded answer. Simple. Precise. And very helpful in general.Polycythemia

© 2022 - 2024 — McMap. All rights reserved.