1

我有一个 UITableView 显示不同部分下的单元格。每个部分中的单元格都可以删除。即,当 UITableView 处于编辑模式时,单元格的样式设置为 UITableViewCellEditingStyleDelete。当用户单击具有此样式的单元格中的删除按钮时,删除确认按钮按预期显示在右侧。自从更新到 iOS 8.1 版后,该行的内容和其上方的部分标题单元格的内容向左倾斜。

在删除模式下,在单击左侧的删除按钮之前

删除模式下的屏幕截图,在单击删除按钮之前

单击左侧的删除按钮后

单击删除按钮后的屏幕截图

在我更新到 iOS 8.1 之前,这一切都很好。有没有其他人遇到过这个?

编辑:相关代码片段

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return [[_store getGroceryAisles] itemCount];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self isCellBeingEdited:indexPath])
    {
        return UITableViewCellEditingStyleNone;
    }
    else
    {
        return (_editIsDeleting) ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert ; 
    }
 }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     NSArray* aisles = [[[self store] getGroceryAisles] list];
     HGGSGroceryAisle* aisle = [aisles objectAtIndex:section];
     return [[aisle grocerySections ] count];
 }
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     NSString *cellIdentifier = @"GrocerySectionCell";
     HGGSGrocerySection * grocerySection;

     grocerySection = [self grocerySectionAt:indexPath inTableView:tableView];
     if ([self isCellBeingEdited:indexPath])
     {
         if ([tableView isEditing])
             cellIdentifier = EDIT_CELL_ID;
         else
         {
             _ipBeingEdited = nil;
         }
        }

        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if ([cellIdentifier  isEqualToString:EDIT_CELL_ID])
        {
              NSString* name = [grocerySection name];
              // skipping other code to set up cell when editing cell....
              _cellBeingEdited = cell;
        }
        else
        {
              [[cell textLabel] setText:[grocerySection name]];

         }
         return cell;
      }

      -(UITableViewCell*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
      {    
          NSString *cellIdentifier;
          HGGSGroceryAisle* aisle;

          if ((tableView == [self tableView]) && (section == 0))
          {
              cellIdentifier = (tableView.isEditing)  ? @"EditngAislesConfigHeaderCell" : @"AislesConfigHeaderCell";
          }
          else
              cellIdentifier =  @"GrocerySectionHeaderCell";

          UITableViewCell *cell= [[self tableView] dequeueReusableCellWithIdentifier:cellIdentifier];   
          UILabel *aisleLabel = (UILabel*)[cell viewWithTag:1] ;

          NSString *aisleLabelText = [NSString stringWithFormat:@"Aisle %li",(long)[aisle number]];
          aisle = [[_store getGroceryAisles] itemAt:section];
          [aisleLabel setText:aisleLabelText];
          [aisleLabel setAccessibilityLabel:aisleLabelText];       

          return cell;
      }

      - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if ((_ipBeingEdited) && (_ipBeingEdited.row == indexPath.row) && (_ipBeingEdited.section == indexPath.section))
              return NO;
          else
              return YES;
      }

      - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
      {
          return NO;
      }
      - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
      {
         if (editingStyle == UITableViewCellEditingStyleDelete)
         {
             // Delete the row from the data source
             [_store removeGrocerySection:[self grocerySectionAt:indexPath inTableView:tableView] fromAisle:[self aisleAt:indexPath inTableView:tableView]];
             [tableView reloadData];
          }   
     }
4

3 回答 3

4

在方法中:

-(UITableViewCell*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    ...
    return cell;
}

尝试返回 contentView,而不是返回单元格对象。

-(UITableViewCell*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    ...
    return cell.contentView;
}

我已经以这种方式解决了我的问题。我正在使用 Swift,但在 Objective-C 中似乎是一样的。

于 2015-03-03T03:28:53.060 回答
0

我的问题是我从“viewForHeaderInSection”返回了一个 UITableViewCell。通过返回单元格的 contentView 来纠正该行为。

有趣的是,原始代码在 iOS 7 中运行良好。我不知道它是设计使然还是偶然。即,由于 UITableCellView 继承自 UIView,我不确定返回 UITableViewCell 是否真的不正确。作为一个仅供参考(以防它帮助其他希望实现自定义页眉和页脚的人),我得到了从How to Implement Custom Table View Section Headers and Footers with Storyboard返回 contentView 属性的想法。

于 2014-11-04T03:27:51.913 回答
-1

试试这个

第一个编辑按钮添加到 ViewControl 顶部的 NavigationBar

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
    [imagearray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


}
    else if(editingStyle == UITableViewCellEditingStyleInsert)
    {

    }
}

我希望它对你有用

于 2014-10-29T05:24:05.210 回答