UITableView insertRowsAtIndexPaths:

Habe ich einen Navigation-Controller, enthält eine uitableview, wenn ich drücken Sie auf eine Zeile, es erscheint eine neue view-controller auf den stack, der verwendet wird, um Detailinformationen in der detail-Ansicht macht es eine Anfrage vom server zu bekommen, einige Antwort dann einmal die Informationen, die zurückgegeben wird, verwende ich insertRowsAtIndexPaths: zum anzeigen der Informationen, die vom server zurückgegeben werden.

Dies alles funktioniert gut, die erste Zeit, dann, wenn ich drücken Sie die zurück-Taste und wählen Sie eine neue Zeile oder der gleichen Zeile für die Anzeige von detaillierten Informationen, sobald ich das insertRowsAtIndexPaths: aufgerufen wird bekomme ich folgende Fehlermeldung:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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).'

Hier ist der code für das drücken der Ansicht auf dem Stapel:

VideoDetailViewController_iPhone *nextView = [[VideoDetailViewController_iPhone alloc] initWithNibName:@"VideoDetailViewController_iPhone" bundle:nil withVideo:rowData];
    nextView.navController = navController;
[navController pushViewController:nextView animated:YES];
[nextView release];

Hier ist der code, der ausgeführt wird, sobald die Informationen, die vom server zurückgegeben

    - (void)fetchVideoDetail:(NSNotification *)notification {
        hasLoadedResponses = YES;

        NSArray *obj = (NSArray *)[notification object];
        responses = [[obj valueForKey:@"responses"] mutableCopy];
        //NSLog(@"RESPONSES: %@", responses);
        if ([responses count] == 0) {
            [tblView reloadData];
            return;
        }

        NSMutableArray *indexes = [[NSMutableArray alloc] init];

        int i = 0;
        for (NSArray *x in responses) {
            if (i > 0) {
                //The reason for skipping the first one is because we will change that row once the table refreshes we just need to insert any rows after the first one.
                [indexes addObject:[NSIndexPath indexPathForRow:i inSection:1]];
            }
            i++;
        }
        //NSLog(@"indexCount: %i", [indexes count]);
        [tblView beginUpdates];
        [tblView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationBottom];
        [tblView endUpdates];
        //[tblView reloadData];
    }

Hier ist die Methoden tableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Return the number of rows in the section.
    if (section == 0) {
        return 1;
    } else {
        if ([responses count] == 0) {
            NSLog(@"numberofrowsinsection: 1");
            return 1;
        } else {
            NSLog(@"numberofrowsinsection: %i", [responses count]);
            return [responses count];
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoCell *cell = (VideoCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];
    if (cell == nil) {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }

    if (indexPath.section == 0) {
            cell.lblTitle.text = [data title];
            cell.lblDescription.text = [data videoDescription];
    } else {
        if ([responses count] == 0) {
            if (!hasLoadedResponses) {
                cell.lblTitle.text = @"";
                cell.lblDescription.text = @"";
            } else {
                //Responses have been loaded
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                cell.lblTitle.text = @"No responses to this video";
                cell.lblDescription.text = @"Be the first to respond by selecting the \"Set as Destination\" button above";
            }
        } else {
            //Display the response information
            cell.lblTitle.text = [[responses objectAtIndex:indexPath.row] valueForKey:@"title"];
            cell.lblDescription.text = [[responses objectAtIndex:indexPath.row] valueForKey:@"description"];
        }
    }

    return cell;
}
  • Ich kann nicht sagen, wenn Sie Hinzugefügt haben, ein neues Feld, um Ihre Antworten zur gleichen Zeit Hinzugefügt eine Zeile in die Tabelle?
  • wenn der view geladen ich habe nur 1 Zeile in Abschnitt 1, der sagt nur laden, dann einmal die Daten geladen ist, wenn die Zeilen in die Tabelle eingefügt werden
  • was ist [Antworten count] an der Zeit, die Sie einfügen der neuen Zeile?
  • die [Reaktionen count] == 0 ist nur damit ich weiß, ob ich nicht eine Antwort, die ich aktualisieren Sie die Tabelle, so zeigt es der text in die vorhandene Zeile, die gesagt hat laden Sie jetzt sagen, keine Antworten gefunden wurden
InformationsquelleAutor dbslone | 2012-01-29
Schreibe einen Kommentar