Miguel
Miguel

Reputation: 2079

UITableView insert row below tapped cell

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

Upvotes: 0

Views: 942

Answers (2)

Avt
Avt

Reputation: 17043

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.

Upvotes: 1

rmaddy
rmaddy

Reputation: 318854

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.

Upvotes: 1

Related Questions