Saturday, June 8, 2013

UITableViewDataSource tableView canEditRowAtIndexPath example in Objective C (iOS).


UITableViewDataSource tableView canEditRowAtIndexPath

Asks the data source to verify that the given row is editable.

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

Parameters
tableView
The table-view object requesting this information.
indexPath
An index path locating a row in tableView.

Return Value of [UITableViewDataSource tableView canEditRowAtIndexPath]
YES if the row indicated by indexPath is editable; otherwise, NO.

Discussion of [UITableViewDataSource tableView canEditRowAtIndexPath]
The method permits the delegate to exclude individual rows from being treated as editable. Editable rows display the insertion or deletion control in their cells. If this method is not implemented, all rows are assumed to be editable. Rows that are not editable ignore the editingStyle property of a UITableViewCell object and do no indentation for the deletion or insertion control. Rows that are editable, but that do not want to have an insertion or remove control shown, can return UITableViewCellEditingStyleNone from the tableView:editingStyleForRowAtIndexPath: delegate method.

UITableViewDataSource tableView canEditRowAtIndexPath example.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.

  NSInteger section = [indexPath section];
  if (section ==0)
    return NO;
  if (section ==1)
    return NO;
  if (section ==2)
    return YES;
  if (section ==3)
    return YES;
  if (section ==4)
    return YES;

 }


- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    [tableView reloadData];
}

Example of [UITableViewDataSource tableView canEditRowAtIndexPath].
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

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

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

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

    BOOL edit = NO;

    if(indexPath.row == 2) {

        edit = YES;
    }

    return edit;
}

End of UITableViewDataSource tableView canEditRowAtIndexPath example article.