iOS 7:自定义UITableViewCell内容在编辑时不会移动吗?

kag*_*noj 8 iphone objective-c ios ios7 xcode5

我正在编辑自定义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
    }
}
Run Code Online (Sandbox Code Playgroud)

Kyo*_*ang 13

您应该将所有自定义单元格的子视图添加到单元格视图中,而不是单元格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;
Run Code Online (Sandbox Code Playgroud)


CW0*_*007 -1

这可能对解决实际问题没有帮助,但我发现您的代码存在一些问题。

首先这个:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int count = [arrData count];
    if(self.editing) [arrData count];
    return count;
}
Run Code Online (Sandbox Code Playgroud)

总是会返回相同的值?你也可能有:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrData count];
}
Run Code Online (Sandbox Code Playgroud)

其次你有:

DeleteUserCell *cell = (BlockedUserCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

您正在创建一个DeleteUserCell 对象,然后将其转换为BlackedUserCell 对象?

至于您的实际问题,当进入编辑模式时,您需要手动移动子视图进行补偿。或者,您可以使用自动布局,它应该可以为您完成此操作,但需要做更多的工作。这里和网上有很多自动布局教程。以下是一些可以帮助您入门的内容:

http://commandshift.co.uk/blog/page/2/

这里有一系列指南,其中包括您正在使用 XIB 时的指南。