I'm working on a Custom UITableViewCell with editing. When I enter editing mode, the label and image are not moved properly.
- (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
}
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With