Niloufar
Niloufar

Reputation: 532

Reorder tableview cell with a custom button

I'm developing an application an I have a UITableView, I am using the following code to manage reordering:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
if(proposedDestinationIndexPath.section==0 && proposedDestinationIndexPath!=sourceIndexPath){

    return proposedDestinationIndexPath;
}
else{
    return sourceIndexPath;
   }
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ 
   return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
  //move rows
}

it works fine, but I want to change the Reorder button position and implement something like Music queue list on iOS 8.4.

I want to change the reorder button to be on the scrollview which contents of the cell are and move with the cell. Screenshot: enter image description here

Thanks in advance :)

Upvotes: 2

Views: 2855

Answers (2)

Niloufar
Niloufar

Reputation: 532

I solved my problem using the code here for reordering and then I used UITableView delegates as follow for swipe to delete.

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //Remove selected cell from tableview and you data source
  }
}

Upvotes: 0

oozywaters
oozywaters

Reputation: 1241

According to this tutorial you can set UITableViewCellReorderControl as a part of UITableViewCellContentView so it will move with the cell's content:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *reorderControl = [cell huntedSubviewWithClassName:@"UITableViewCellReorderControl"];
    UIView *contentView = [cell huntedSubviewWithClassName:@"UITableViewCellContentView"];

    UIView *resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(reorderControl.frame), CGRectGetMaxY(reorderControl.frame))];
    [resizedGripView addSubview:reorderControl];
    [contentView addSubview:resizedGripView];

    //  Original transform
    CGAffineTransform transform = CGAffineTransformIdentity;

    //  Move reorderControl to the left with an arbitrary value
    transform = CGAffineTransformTranslate(transform, -100, 0);

    [resizedGripView setTransform:transform];
}

Upvotes: 1

Related Questions