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."
– CCastro
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];
– CCastro
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
– CCastro
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.
– Clara Frazão
Does he give any error messages? What is [self spotsWithSection: indexPath.Section]; Right would not be [self.spotsWithSection objectAtIndex: indexPath.Section]; ?
– CCastro
[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.
– Clara Frazão
Now it’s clearer. but
– CCastro