Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 : Custom UITableViewCell content doesn't move expected on editing?

I'm working on a Custom UITableViewCell with editing. When I enter editing mode, the label and image are not moved properly.

enter image description here

- (IBAction) EditTable:(id)sender{
    UIButton *btn = (UIButton *)sender;
    if(self.editing) {
        [super setEditing:NO animated:NO];
        [btn setTitle:@"edit" forState:UIControlStateNormal];
        [tblView setEditing:NO animated:NO];
    } else {
        [super setEditing:YES animated:YES];
        [btn setTitle:@"done" forState:UIControlStateNormal];
        [tblView setEditing:YES animated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int count = [arrData count];
    if(self.editing) [arrData count];
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    DeleteUserCell *cell = (DeleteUserCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DeleteUserCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
    cell.imgCell.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", indexPath.row+1]];
    cell.lblCell.text = [arrData objectAtIndex:indexPath.row];
    return cell;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
   return YES;
}
// Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [arrData removeObjectAtIndex:indexPath.row];
        [tblView reloadData];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}
like image 579
kagmanoj Avatar asked Jan 22 '14 12:01

kagmanoj


1 Answers

You should add all of custom cell's subviews to not cell's view but cell's contentView.

If you make the custom cell by Interface Builder, you can find contentView of the custom cell in interface builder easily. Otherwise, if making it without Interface Builder, check UITableViewCell who has contentView property.

EDIT: FYI, check the following comment in UITableViewCell.

// If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.
@property (nonatomic, readonly, retain) UIView      *contentView;
like image 153
Kyokook Hwang Avatar answered Nov 15 '22 17:11

Kyokook Hwang