Mohsen Shakiba Mohsen Shakiba - 3 months ago 19
Swift Question

Swift - 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.

summery:

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.

thanks in advance

Answer

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