iphone - 如何同时从 UITableView 插入和删除一行

标签 iphone ios uitableview

我想让 UITableView 像 Accordion 一样。当一行被点击时,它应该在被点击的行的正下方插入一个特殊的行,然后从之前的点击中删除任何其他特殊的行。我已经尝试了很多东西,但下面的代码是我最近的尝试。

- (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];

}

关于iphone - 如何同时从 UITableView 插入和删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9098423/

相关文章:

ios - 我可以根据分段控制索引通过按钮快速移动屏幕吗?

iphone - 通过 android 和 iPhone 应用程序集成 yahoo、Google 和 openid?

ios - 删除后退栏按钮标题 Swift

ios - 加载 subview 触发根 ViewController

ios - 将 indexPath.row 发送到另一个 UIViewController

ios - bool 字段 UIAlertController swift

iphone - 应用程序在 CoreData 保存时卡住

objective-c - 用于 objective-c 的 Twitter SDK

ios - 通知中心未在我的 obj c 类( utills 类或对象类)中触发。不是 View Controller

ios - UITableView 标题中的按钮仅在 1/3 的时间内起作用