Tableview, delete cell but not section

Asked

Viewed 79 times

2

I have a View table where I would like to delete the cells from the sections and if it is the last one, instead of deleting the section as well, I would like you to add a new cell with a new string: @"there are no cells in this section". For some reason the code below is not working.

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSMutableArray *spotType = [self spotsWithSection: section];

return spotType.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSUInteger section = indexPath.section;
NSUInteger row = indexPath.row;

NSMutableArray *thisSpotContainer = [self spotsWithSection:section];
Spot *s = (Spot*)thisSpotContainer[row];
cell.textLabel.text = s.name;

return cell;
}




- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {

    NSMutableArray *thisSpotContainer = [self spotsWithSection: indexPath.section];

    if ([thisSpotContainer count] > 0) {
        [tableView beginUpdates];

        [thisSpotContainer removeObjectAtIndex:[indexPath row]];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


        if ([thisSpotContainer count] == 0) {

            NSArray *noSpotsHere = [NSArray arrayWithObjects:@"This section has no spots.", nil];
            [thisSpotContainer insertObject:noSpotsHere atIndex:0];

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }

        [tableView endUpdates];
    }

}

}

- (NSMutableArray*) spotsWithSection:(NSUInteger)section {

NSArray * Spots = [Spot allSpots];
NSPredicate *predicate;

switch (section) {
    case 0:
        predicate = [NSPredicate predicateWithFormat:@"type = %@", @"restaurant"];
        spotRest = [[Spots filteredArrayUsingPredicate:predicate] mutableCopy];
        return spotRest;
        break;
    case 1:
        predicate = [NSPredicate predicateWithFormat:@"type = %@", @"bar"];
        spotBar = [[Spots filteredArrayUsingPredicate:predicate] mutableCopy];            return spotBar;
        break;
    case 2:
        predicate = [NSPredicate predicateWithFormat:@"type = %@", @"club"];
        spotClub = [[Spots filteredArrayUsingPredicate:predicate] mutableCopy];
        return spotClub;
        break;
}
}
  • I understood that spotsWithSection is an array array, so the amount of elements in it indicates the amount of sections and arrays within them, the lines of each section. However, I think q vc was wrong here: Nsarray *noSpotsHere = [Nsarray arrayWithObjects:@"This Section has no spots." , nil]; [thisSpotContainer insertObject:noSpotsHere atIndex:0]; Vc is inserting an array within thisSpotContainer element 0 when actually it would have to insert only the text "This Section has no spots."

  • In short, delete the lines: Nsarray *noSpotsHere = [Nsarray arrayWithObjects:@"This Section has no spots." , nil]; [thisSpotContainer insertObject:noSpotsHere atIndex:0]; and enter [thisSpotContainer insertObject:@"This Section has no spots." atIndex:0];

  • ah não ser q vc is really working with a 3 dimensional array... To understand better, you could put your numberOfRowsInSection and numberOfSectionsInTableView and cellForRowAtIndexPath

  • spotsWithSection is an array of objects and the amount of elements within it indicates the number of Rows in that section. I tried to put the string in place of the object but it still doesn’t add.

  • Does he give any error messages? What is [self spotsWithSection: indexPath.Section]; Right would not be [self.spotsWithSection objectAtIndex: indexPath.Section]; ?

  • [self spotsWithSection: indexPath.Section] is a method that returns an array of the spots divided by types, ready to be inserted in the appropriate sections: restaurant, bar or club. I edited on top.

  • Now it’s clearer. but

Show 2 more comments

1 answer

2


Apparently, these lines have no bearing on anything:

 NSArray *noSpotsHere = [NSArray arrayWithObjects:@"This section has no spots.", nil];
[thisSpotContainer insertObject:noSpotsHere atIndex:0];

Since in cellForRowAtIndexPath vc is loading the array again of something q looks like a bank, overwriting what you inserted into it.

What you can do is, in the commitEditingStyle after:

   [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

place:

  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  cell.textLabel.text = @"This section has no spots.";

However, the other problem is that if a reloadData occurs the line will disappear.

What I suggest to do is to place a reloadData after deletion:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
      if (editingStyle == UITableViewCellEditingStyleDelete) {
          NSMutableArray *thisSpotContainer = [self spotsWithSection: indexPath.section];
         if ([thisSpotContainer count] > 0) {
            [tableView beginUpdates];
            [thisSpotContainer removeObjectAtIndex:[indexPath row]];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            if ([thisSpotContainer count] == 0) {
                 [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
               cell.textLabel.text = @"This section has no spots.";
            }
         }
         [tableView endUpdates];
    }
    [tableview reloadData]
}

and:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSUInteger section = indexPath.section;
    NSUInteger row = indexPath.row;

    NSMutableArray *thisSpotContainer = [self spotsWithSection:section];
    if (thisSpotContainer.count == 0) {
       cell.textLabel.text = @"This section has no spots.";
    }else{
        Spot *s = (Spot*)thisSpotContainer[row];
        cell.textLabel.text = s.name;
    }
  return cell;
}

and in the

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     NSMutableArray *spotType = [self spotsWithSection: section];
     int linhas = spotType.count;
     if (linhas == 0) {
        linhas = 1;
     }
     return linhas;
}
  • I forgot the numberOfRowsInSection, I made an edit. I am responding without my mac around, so may have some error.

  • The first option works, however the second one doesn’t yet. Can you post the commitEditingStyle code with the second option? Anyway thank you so much.

  • Placed the commitEditingStyle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.