0

I'm extremely confused. I have looked at several posts to try to figuere out how to do this. What i would like to do is detect when a UITableViewCell is tapped and then insert a row right below that row with a menu.

I implemented detecting the tapped item but I can't figure out how to insert the new row right below the row that was just tapped.

Here is the method that handles the tapped cell.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    indexPath = nil;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Locations *location = nil;


    if (self.searchDisplayController.active) {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        location= [self.searchResults objectAtIndex:indexPath.row];

        NSLog(@"location : %@" ,location.locationName);
        [self.locations addObject:@"New Entry"];


        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];

        //[self.tableViewForScreen insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    } else {
        indexPath = [self.tableView indexPathForSelectedRow];
        location = [self.locations objectAtIndex:indexPath.row];
         NSLog(@"location : %@" ,location.locationName);
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
    }


    [tableView deselectRowAtIndexPath:indexPath animated:YES];



     // set the tapped item pointer equals to the locations mutable array variable wich essentially is the
    //  data source of the table view.
    //Locations *tappedItem =[self.locations objectAtIndex:indexPath.row];

    //access the location name and print to log.



}

Obviously it fails at the part of insert

[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];

Can someone give me a hand?

The error is the following.

reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (154) must be equal to the number of rows contained in that section before the update (154), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Here's a post that I looked at. UITableView action row http://adcdownload.apple.com//videos/wwdc_2011__sd/session_125__uitableview_changes_tips_tricks.m4v

Community
  • 1
  • 1
Miguel
  • 2,019
  • 4
  • 29
  • 53

2 Answers2

1

The problems is that you call insertRowsAtIndexPath: but you don't first update the data source to include the data for the new row.

You seem to do this correctly when selecting a row in the search table but not the main table.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks maddy i kind of though that but arent i doing that here `[self.locations addObject:@"New Entry"];` – Miguel Feb 28 '14 at 22:49
  • But that's for the search table. You don't do that for the main table (inside the `else` part of your `if` statement). – rmaddy Feb 28 '14 at 22:50
  • thank you maddy that is exactly what the problem was. one more question if you dont mind i like to insert a custom cell right after the tapped cell. now im just picking up the indexpath of the current cell how can i say like indexpath+1? – Miguel Feb 28 '14 at 23:28
  • 1
    Create a new indexPath: `NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];`. – rmaddy Feb 28 '14 at 23:38
1

First of all do not use

indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];

you can take indexPath from method parameters

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

It seems that you have problems because you are calling insertRowsAtIndexPaths but you are not updating data source.

Avt
  • 16,927
  • 4
  • 52
  • 72