Search code examples
iosobjective-cios6uitableview

How to move uitableview custom cell?


I use custom cell.I want to move this cell (change position of the cells)

i used this delegations

UITableViewDataSource,UITableViewDelegate

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *Cellidentifier = @"Celledit";
CellEdit *editCell =[tableView dequeueReusableCellWithIdentifier:Cellidentifier];

if (!editCell) {
    editCell = [[CellEdit alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
}

MItem *mItem = [mItems objectAtIndex:[indexPath row]];


 ---------------------------------
 // set data 3 labales in the cell
 --------------------------------- 

editCell.editdelegate = self;
return editCell;

editcell is my custom cell

[_tableViewEdit setEditing:YES animated:YES]; this line put in - (void)viewDidLoad

i want to edit it always

    // table view change cells
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

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

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    NSInteger sourceRow = sourceIndexPath.row;
    NSInteger destRow = destinationIndexPath.row;
    id object = [minutesItems objectAtIndex:sourceRow];

    [minutesItems removeObjectAtIndex:sourceRow];
    [minutesItems insertObject:object atIndex:destRow];

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    for (UIControl *control in cell.subviews)
    {
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellReorderControl")] && [control.subviews count] > 0)
        {
            for (UIControl *someObj in control.subviews)
            {
                if ([someObj isMemberOfClass:[UIImageView class]])
                {
                    UIImage *img = [UIImage imageNamed:@"logocoffee.png"];
                    ((UIImageView*)someObj).frame = CGRectMake(0.0, 0.0, 43.0, 43.0);
                    ((UIImageView*)someObj).image = img;
                }
            }
        }
    }
}


}

to move the cell simulator not showing right side control icon

why this? that means cant move costume cells?


Solution

  • Custom cells can be moved as well.

    From your code samples we don't see where you are setting your tableview in the editing mode.

    Perhaps the issue is because you are never setting the editing mode.

    If so, try to add an Edit button in the title bar for example, which should be linked to a method such as:

    - (IBAction) EditTable:(id)sender{
     if(self.editing)
     {
        [super setEditing:NO animated:NO]; 
        [yourTableView setEditing:NO animated:NO];
        [yourTableView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
     }
     else
     {
        [super setEditing:YES animated:YES]; 
        [yourTableView setEditing:YES animated:YES];
        [yourTableView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
     }
    }
    

    I think this one should resolve your issue.

    Edit: To resume in your case, as a minimum, you only need to set your table view in the editing mode in viewDidLoad, and to update it if needed:

    [yourTableView setEditing:YES animated:YES];
    [yourTableView reloadData];
    

    and to implement both callbacks:

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
         //Even if the method is empty you should be seeing both rearrangement icon and animation.
    }
    

    I tried with custom cells and it's working fine.