arachide
arachide

Reputation: 8066

How do I disable the delete operation for certain row in a UITableView?

I know about using setEditing: to enable the editing mode of UITableView.

But I prefer to disable the operation for some certain rows (enable other rows).

Is it possible?

Thanks,

interdev

Upvotes: 19

Views: 12135

Answers (4)

Sethuraghavan
Sethuraghavan

Reputation: 56

Based on your question, I understand that you want to just hide the delete button from appearing on the 1st row. please set tableview editing to yes and add the below code

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.row==0)
    {
        return NO;
    }
    return YES;
}

Upvotes: 3

Charlie Wu
Charlie Wu

Reputation: 7757

this post sum it up perfectly, Is there any way to hide "-" (Delete) button while editing UITableView

in case you don't want to read the post the gist is on the row you don't want to delete but want to allow move row: set edit style for row to UITableViewCellEditingStyleNone

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

Upvotes: 10

Youyou
Youyou

Reputation: 111

Using canEditRowAtIndexPath will not allow the row to align with other cells in grouped style. which can look bad.

I use UITableViewCellEditingStyleNone, for the rows which you do not want to show the edit control (minus/plus button), put this in your

(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{}

Upvotes: 9

kennytm
kennytm

Reputation: 523294

Implement the -tableView:canEditRowAtIndexPath: method in your data source. Return NO for those rows.

Upvotes: 36

Related Questions