删除TableView行使用编辑/删除按钮

时间:2013-11-06 16:05:10

标签: ios objective-c uitableview

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

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

1 个答案:

答案 0 :(得分: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]];
    }
}