【问题标题】:iOS 7 : Custom UITableViewCell content doesn't move expected on editing?iOS 7:自定义 UITableViewCell 内容在编辑时不会移动?
【发布时间】:2014-02-12 12:54:11
【问题描述】:

我正在使用自定义 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
    }
}

【问题讨论】:

  • 您好,您在为这个项目使用 Autolayout 吗?
  • 分享您的单元格故事板布局视图将帮助我们找出问题所在。
  • @bhavyakothari 不,我没有在这个项目中使用自动布局... :(

标签: ios iphone objective-c ios7 xcode5


【解决方案1】:

我在 Xcode 4.5 中看到过这个错误。 IB 将自动布局约束与单元格连接,而不是与单元格的 contentView。在 Xcode 5 中,这个问题已经消失了。如果之前在 Xcode 4.5 中创建了约束,您只需要重新连接约束即可。

【讨论】:

    【解决方案2】:

    当用户滑动单元格时调用以下代码
    因此,您可以按如下方式配置单元格内容

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textAlignment = NSTextAlignmentRight;
    
    // set frames for all content you have got for that cell
    
    UITableViewCellEditingStyle *style = UITableViewCellEditingStyleDelete;
    
    return style;
    
    }
    

    【讨论】:

      【解决方案3】:

      解决此问题的另一个有用方法是处理这些事件:

      tableView:willBeginEditingRowAtIndexPath
      tableView:didEndEditingRowAtIndexPath
      

      并为各个控件设置适当的框架。

      希望对你有帮助!

      【讨论】:

        【解决方案4】:

        根据您的代码结构,您可以采取一些措施来修复它。从提供的代码 sn-p 中,我不清楚您的单元格的自定义 nib 文件是如何使用的。

        • 首先确保自定义单元格中的图像不包含在附件视图中。要了解有关创建自定义表格视图单元格的更多信息,请浏览以下link

        • 其次,确保自定义 tableCell 的 UIView 的大小与编辑模式下的表格视图内容大小相同。点击编辑时,您可能需要重新定位图像。您可以通过以下方法检查何时发生编辑:

          • (void)setEditing:(BOOL)editing Animation:(BOOL)animated

        也就是说,我会尽可能避免在单元格内重新定位项目。如果单元格是使用 autoLayout 正确创建的,并且它在没有编辑模式的情况下很好地显示,那么它将在编辑模式下自动调整。这里是 link 以阅读有关自动布局的更多信息。

        【讨论】:

          【解决方案5】:

          如果您在 DeleteUserCell.xib 中使用自动布局
          查看您的约束并尝试在 Table View Cell 中执行编辑器->重置为建议的约束。 尝试调整单元格的框架以查看单元格在编辑模式下的外观。

          如果您没有在 DeleteUserCell.xib 中使用自动布局
          查看您的单元格子视图的自动调整大小掩码。确保当他们的 superview 发生变化时他们会正确调整他们的框架。

          【讨论】:

            【解决方案6】:

            您应该将所有自定义单元格的子视图添加到单元格的视图而不是单元格的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;
            

            【讨论】:

            • 我在 IB 中创建了我的自定义单元格布局,它位于 contentView 中,但它仍然不会自动生成动画。也许我需要适当地设置约束?
            • @Fraggle 我不明白你到底在遭受什么。单元格的哪个元素没有动画?让我知道更多详细信息?
            • @Kyokook_Hwang,我想我明白了。即使我的视图在 contentView 内,这些视图的约束也并非全部链接到 contentView。删除约束,然后从视图中控制拖动到 IB 中的 contentView 以创建新约束,它现在似乎正在工作。
            【解决方案7】:

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

            首先是这个:

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

            总是返回相同的值吗?你也可以:

            - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
            {
                return [arrData count];
            }
            

            其次你有:

            DeleteUserCell *cell = (BlockedUserCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            

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

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

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

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

            【讨论】:

              猜你喜欢
              • 2013-10-12
              • 1970-01-01
              • 2012-11-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多