Правильное удаление элемента из UITableView - PullRequest
1 голос
/ 19 марта 2012

Может ли кто-нибудь объяснить, как правильно реализовать метод tableView:commitEditingStyle:forRowAtIndexPath:, чтобы я мог правильно удалить элемент из резервного массива и отобразить только те элементы, которые я еще не удалил?

Вот что у меня есть:

@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UITableView *tableView;

    NSArray *allItems;
    NSMutableArray *displayItems;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    allItems = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven", nil];
    displayItems = [[NSMutableArray alloc] initWithArray:allItems];
}

-(UITableViewCell*) tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
    return cell;
}

@end

Ответы [ 2 ]

3 голосов
/ 19 марта 2012

Реализуйте ваш tableView:commitEditingStyle:forRowAtIndexPath: следующим образом:

// Override to support editing the table view.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // remove the object from display array
        [displayItems removeObjectAtIndex:indexPath.row];

        // Delete the row from the data source.
        [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
3 голосов
/ 19 марта 2012

Попробуйте это:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        if(!deletedIndices) deletedIndices = [[NSMutableIndexSet alloc] initWithIndex:indexPath.row];
        else [deletedIndices addIndex:indexPath.row];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}

-(IBAction)editingDidComplete:(id)sender
{

    // remove the objects from display array
    [valueArray removeObjectsAtIndexes:deletedIndices];
    // Reload tableView if needed
    [mainTableView reloadData];
    [mainTableView setEditing:NO];
 }
...