Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable editing mode just for a section in UITableView

I have a tableView in which there is a section that is editable.

If I enable editing on the entire tableView, other cells while in editing mode are not selectable, so I need to enable the editing mode only on a specific section so that while other cells are selectable, the section is editable.

The reason that I need to set editing is those red square minus buttons that appear next to deletable cells.

Summary:

I need those red minus buttons next to cells, so I need to set editing as true, but if I do so, other cells won't be selectable thus I need to either set editing as true for a specific section, or add those red minus buttons without the editing mode.

like image 350
Mohsen Shakiba Avatar asked Aug 30 '16 07:08

Mohsen Shakiba


2 Answers

You can implement canEditRowAtIndexPath method something like,

Obj-C

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


if (indexPath.section == 1) {

    return YES;
}

return NO;
}

Swift :

   func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {

    if indexPath.section == 1 {
        return true
    }


    return false
}

To enable selection during editing you need to set,

Obj-C

     self.yourTableView.allowsSelectionDuringEditing = YES;

Swift

     self.yourTableView.allowsSelectionDuringEditing = true
like image 68
Ketan Parmar Avatar answered Nov 14 '22 23:11

Ketan Parmar


Swift 4/5

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if indexPath.section == 1 {
            return true
        }
        return false
    }
like image 44
LaoHan Avatar answered Nov 14 '22 23:11

LaoHan