0

我正在尝试删除数组中的行,从点击删除按钮,点击编辑后,然后点击红色减号小按钮。当我在模拟器上点击删除按钮时,什么也没有发生。如果有人可以帮助我,请。这是我的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    //here is where the magic doesn't happens
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [stories removeObjectAtIndex: storyIndex];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"module"];

    // clean up the link - get rid of spaces, returns, and tabs...
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

    NSLog(@"link: %@", storyLink);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
}
4

1 回答 1

0

您应该执行以下操作..

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    //here is where the magic doesn't happens
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [stories removeObjectAtIndex: storyIndex];

        //EDIT
        [tableView beginUpdates]; 
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [tableView endUpdates];

    }else{ //EDIT

         NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"module"];

        // clean up the link - get rid of spaces, returns, and tabs...
        storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
        storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

        NSLog(@"link: %@", storyLink);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
    }
}
于 2013-11-06T16:18:40.763 回答