1

I have a UITableView showing cells under different sections. The cells within each section can be deleted. I.e., when the UITableView is in edit mode, the cells' styles are set to UITableViewCellEditingStyleDelete. When the user clicks the delete button within a cell with this style, the Delete confirmation button shows to the right as expected. Since updating to iOS version 8.1, the row's contents and the contents of the section header cells above it are skewed to the left.

In Delete Mode, before clicking on delete button on the left hand side

Screenshot in delete mode, before clicking delete button

After clicking on delete button on the left hand side

Screenshot after clicking on delete button

This was working fine up until I updated to iOS version 8.1. Has anyone else run into this?

Edit: Relevant snippets of code

- (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];
          }   
     }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ana M
  • 467
  • 5
  • 13
  • Is there a specific reason to why you want to have your "done"-button in the tableview section header? – Mani Oct 30 '14 at 12:07
  • The "Done" button is used to change back from either the regular edit mode or the delete mode, back to the view only mode. It is only present in the first section header. I guess I could put it the table header instead... I did at one time; cant recall why I moved it a while back to the first section header. However, moving it to the table header would not solve my problem - I typically have other section headers, all of which get skewed when the delete button is pressed. – Ana M Oct 31 '14 at 00:23

3 Answers3

4

in the method:

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

Instead of return the cell object, try to return the contentView.

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

I have solved my problem in that way. I am using Swift, but it seems the same way in Objective-C.

Bruno Paulino
  • 5,611
  • 1
  • 41
  • 40
  • 1
    Thanks! all tutorials tell me to return a cell object! but the new problem is that the footer view is not displayed? – c9s Mar 09 '15 at 16:12
  • Have you implemented this method: func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? see this link: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:viewForFooterInSection: – Bruno Paulino Mar 14 '15 at 01:27
0

My problem was that I as returning a UITableViewCell from "viewForHeaderInSection". The behavior was corrected by returning the contentView of the cell instead.

Interesting that the original code worked fine in iOS 7. I don't know if it was working by design or by accident. I.e., since UITableCellView inherits from UIView, I am not certain that returning a UITableViewCell is really incorrect. As an fyi (in case it helps someone else looking to implement custom headers and footers), I got the idea of returning the contentView property from How to Implement Custom Table View Section Headers and Footers with Storyboard.

Community
  • 1
  • 1
Ana M
  • 467
  • 5
  • 13
-1

Try this

First Edit button add to NavigationBar in Top of ViewControl

 -(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)
    {

    }
}

I hope it's Work for you

Pavan Alapati
  • 317
  • 1
  • 5
  • 17
  • I already have this method implemented = the actual delete is working fine. It is just the display that is messed up when the delete button (on the right hand side) is tapped. – Ana M Oct 29 '14 at 11:01
  • @AnaM go through this like it help to You http://adeem.me/blog/2009/05/29/iphone-sdk-tutorial-add-delete-reorder-uitableview-row/ – Pavan Alapati Oct 30 '14 at 06:23
  • Seems to be an incorrect management of indexPath inside method proposed by Pavan. Please post your code so we can see what you wrote. – dpizzuto Oct 30 '14 at 09:50
  • I just added the code that I believe is relevant to the problem. – Ana M Oct 31 '14 at 01:03
  • It is not a problem with commitEditingStyle method. – Bruno Paulino Mar 23 '15 at 23:20