Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling edit mode for only one row in table view

I can set the edit mode for a while uitableview by calling [tableView setEditing:YES]; But that sets the edit mode for all the rows in a table.

Is there a way to detect what row was swiped an enable the edit mode only for that row?

Thanks

like image 912
msk Avatar asked Mar 12 '10 22:03

msk


1 Answers

I haven't coded this up, but here's the idea.

Create selectionIndexPath in .h

NSIndexPath *selectionIndexPath;

Then in .m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

save indexPath to selectionIndexPath and call:

[self.tableView setEditing:YES];

Then in:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

if (selectionPath.row == indexPath.row)
    {
        return UITableViewCellEditingStyleDelete;
    }
    else
    {
        return UITableViewCellEditingStyleNone;     
    }
}

You could also catch the touches and then do more or less the same thing. Something like this...

NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
        // save indexPath and setEditing Mode here
}

Didn't have any time to code it up, but that's the main idea.

like image 103
Jordan Avatar answered Oct 04 '22 00:10

Jordan