我正在编辑自定义UITableViewCell.进入编辑模式时,标签和图像无法正常移动.
- (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 } }
解决方法
您应该将所有自定义单元格的子视图添加到单元格的视图中,而不是单元格的contentView.
如果通过Interface Builder创建自定义单元格,则可以轻松地在界面构建器中找到自定义单元格的contentView.否则,如果没有Interface Builder,请检查具有contentView属性的UITableViewCell.
编辑:
仅供参考,请在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;