Tomasz Szulc
Tomasz Szulc

Reputation: 4235

How to reorder Cells in UITableView

i try about hour to make ability to reoder Cells in UITableView.

i create cells

-(UITableViewCell *)tableView:(UITableView *)tV cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tV dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Set up the cell...



cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.showsReorderControl = YES;
return cell;
}

and i've got this:

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

and button method

- (IBAction)delete:(id)sender {
if (self.btnLeft.tag == 0) {
    self.btnLeft.title = @"Zakończ";
    [self.tableView setEditing:YES animated:YES];
    self.btnLeft.tag = 1;
}
else if (self.btnLeft.tag == 1) {
    self.btnLeft.title = @"Edytuj";
    [self.tableView setEditing:NO animated:YES];
    self.btnLeft.tag = 0;
}
}

but when i click Edit button i setEditing to YES, but i can only delete rows, not reorder. when i've got error? I mean, no reorder icon on right cell's side.

Upvotes: 0

Views: 1891

Answers (1)

Tomasz Szulc
Tomasz Szulc

Reputation: 4235

Ok, i solved it.

For the reordering control to appear, you must not only set this property but implement the UITableViewDataSource method tableView:moveRowAtIndexPath:toIndexPath:. In addition, if the data source implements tableView:canMoveRowAtIndexPath: to return NO, the reordering control does not appear in that designated row.

Upvotes: 1

Related Questions