簡體   English   中英

如何同時在UITableView中插入和刪除行

[英]How to insert and remove a row from UITableView at the same time

我想讓UITableView像手風琴一樣演奏。 輕敲一行時,應在該輕敲行的正下方插入一個特殊行,然后從以前的水龍頭中刪除任何其他特殊行。 我嘗試了很多事情,但是下面的代碼是我的最新嘗試。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *removeIndex;
    for (int i = 0; i < [players count]; i++) {
        NSString *player = [players objectAtIndex:i];
        if ([player isEqualToString:@"ADJUST_SCORE_ROW"]) {
            removeIndex = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            [players replaceObjectAtIndex:i withObject:@"DELETE_ME"];
            break;
        }
    }

    [scoreTableView beginUpdates];
    NSIndexPath *insertPath;
    if (removeIndex && [removeIndex row] < indexPath.row) {
        insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
    } else {
        insertPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
    }
    [players insertObject:@"ADJUST_SCORE_ROW" atIndex:insertPath.row];

    [scoreTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];

    for (int i = 0; i < [players count]; i++) {
        NSString *player = [players objectAtIndex:i];
        if ([player isEqualToString:@"DELETE_ME"]) {
            [players removeObject:player];
            break;
        }
    }

    if (removeIndex) {
        [scoreTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:removeIndex] withRowAnimation:UITableViewRowAnimationTop];
    }

    [scoreTableView endUpdates];


}

關鍵在於刪除和插入發生的位置。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Player *selectedPlayer = nil;

    //Get rid of any adjustment score row
    NSIndexPath *removeIndex;
    for (int i = 0; i < [players count]; i++) {
        id player = [players objectAtIndex:i];
        if ([player isKindOfClass:[NSString class]] && [player isEqualToString:@"ADJUST_SCORE_ROW"]) {
            if (i == indexPath.row) {
                [scoreTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                return;  //This is the case where the row that was selected was an adjustment row
            }
            removeIndex = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            [players replaceObjectAtIndex:i withObject:@"DELETE_ME"];
            break;
        }
    }

    selectedPlayer = [players objectAtIndex:indexPath.row];

    [scoreTableView beginUpdates];
    NSIndexPath *insertPath;
    if (removeIndex && [removeIndex row] < indexPath.row) {
        insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
    } else {
        insertPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
    }

    HoleScore *holeScore = [[round scoreForPlayer:[players objectAtIndex:indexPath.row] holeGroup:self.holeGroupIndex andHole:hole.holeIndex] objectForKey:CURRENT_HOLE_SCORE];
    if (holeScore == nil) {

        NSEntityDescription *scoreEntity = [NSEntityDescription entityForName:@"HoleScore" inManagedObjectContext:moc];
        holeScore = [[HoleScore alloc] initWithEntity:scoreEntity insertIntoManagedObjectContext:moc];
        [holeScore setPlayer:selectedPlayer];
        [holeScore setHoleGroupIndex:[NSNumber numberWithInteger:holeGroupIndex]];
        [holeScore setHoleIndex:[NSNumber numberWithInteger:hole.holeIndex]];

        [holeScore setStrokes:[NSNumber numberWithInteger:[hole par]]];
        [holeScore setPuts:[NSNumber numberWithInteger:2]];

        HoleScoreCardTableViewCell *playerCell = (HoleScoreCardTableViewCell*)[scoreTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
        [playerCell setHoleScore:holeScore];

        [round addScoresObject:holeScore];

        NSError *error;
        [moc save:&error];

        if (error) {
            NSLog(@"Error saving: %@", error.localizedDescription);
        }

    }

    for (int i = 0; i < [players count]; i++) {
        id player = [players objectAtIndex:i];
        if ([player isKindOfClass:[NSString class]] && [player isEqualToString:@"DELETE_ME"]) {
            [players removeObject:player];
            break;
        }
    }

    if (removeIndex) {
        [scoreTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:removeIndex] withRowAnimation:UITableViewRowAnimationTop];
    }

    [players insertObject:@"ADJUST_SCORE_ROW" atIndex:insertPath.row];

    [scoreTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];

    [scoreTableView endUpdates];

}

您必須為委托方法更新節數和相應行數...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM