使用commitEdittingStyle删除tableview行和部分为空

时间:2014-11-04 00:31:46

标签: ios objective-c uitableview tableview

我似乎无法对此发现太多。我目前正在尝试创建一个可刷新的删除按钮,该按钮将删除刷过的行,如果该行现在是空的,则它也将删除该节标题。例如,"面包"被滑动以删除,并且在标题标题下没有其他内容" B"。然后这将删除面包和" B"节标题。我的代码如下。

@interface ChoicesTableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *items;
@property (strong, nonatomic) NSMutableDictionary *alphabetizedItems;

@end

@implementation ChoicesTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    self.items = [[NSMutableArray alloc] init];
    [self.items addObject:@"Apples"];
    [self.items addObject:@"Bread"];
    self.alphabetizedItems = [self alphabetizeItems:self.items];
}

//Segue if the item is tapped
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    MyDataChoices *currentRow = self.arrayNames[indexPath.row];
//    self.mySelectedCell = currentRow.myNameChoices;
//    
//    [self performSegueWithIdentifier:@"unwindSegueAction" sender:self];
//    
//}
////unwind segue from add choice
- (IBAction)unwindSegueToChoices:(UIStoryboardSegue *)segue
{

    AddChoiceViewController *sourceVC = segue.sourceViewController;
    NSString *myNewItem = sourceVC.myTextField.text;
    //NSString *myFinalString = [[myNewItem substringToIndex:1] capitalizedString];
    NSString *stringCapitalized = [myNewItem capitalizedString];
    [self.items addObject:stringCapitalized];
    self.alphabetizedItems = [self alphabetizeItems:self.items];
    //[self.arrayNames addObjectsFromArray:@[[MyDataChoices itemWithNewName:stringCapitalized]]];
    [self.tableView reloadData];

}
//titles for talble view
#pragma mark Helper Methods
- (NSMutableDictionary *)alphabetizeItems:(NSArray *)items {
    NSMutableDictionary *buffer = [[NSMutableDictionary alloc] init];

    // Put Fruits in Sections
    for (int i = 0; i < [items count]; i++) {
        NSString *fruit = [items objectAtIndex:i];
        NSString *firstLetter = [[fruit substringToIndex:1] uppercaseString];

        if ([buffer objectForKey:firstLetter]) {
            [(NSMutableArray *)[buffer objectForKey:firstLetter] addObject:fruit];

        } else {
            NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:fruit, nil];
            [buffer setObject:mutableArray forKey:firstLetter];
        }
    }

    // Sort Fruits
    NSArray *keys = [buffer allKeys];
    for (int j = 0; j < [keys count]; j++) {
        NSString *key = [keys objectAtIndex:j];
        [(NSMutableArray *)[buffer objectForKey:key] sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }

    NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:buffer];
    return result;
}
#pragma mark title indexing

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *keys = [[self.alphabetizedItems allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [keys objectAtIndex:section];
    return key;
}



# pragma mark main table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView
{
    NSArray *keys = [self.alphabetizedItems allKeys];
    return [keys count];
}


-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
    //return self.arrayNames.count;
    NSArray *unsortedKeys = [self.alphabetizedItems allKeys];
    NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [sortedKeys objectAtIndex:section];
    NSArray *fruitsForSection = [self.alphabetizedItems objectForKey:key];
    return [fruitsForSection count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //MyDataChoices *currentRow = self.arrayNames[indexPath.row];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell2" forIndexPath:indexPath];

    //cell.textLabel.text = currentRow.myNameChoices;
    NSArray *unsortedKeys = [self.alphabetizedItems allKeys];
    NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
    NSArray *fruitsForSection = [self.alphabetizedItems objectForKey:key];
    NSString *fruit = [fruitsForSection objectAtIndex:[indexPath row]];

    [cell.textLabel setText:fruit];

    return cell;

}

# pragma Mark delete slide button

//Delete Swipe Button
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// 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
        int index = indexPath.row;
        //[self.items removeObjectAtIndex:index];
        [self.alphabetizedItems removeObjectForKey:indexPath];
        [tableView deleteRowsAtIndexPaths:@[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
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

1 个答案:

答案 0 :(得分:3)

基本方法是查看要删除的行的部分中有多少行。如果该部分有两行或更多行,只需删除您现在正在执行的行。如果该部分只有一行(被删除的那一行),则从数据模型中删除该部分,然后从表中删除该部分而不是删除该行。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSArray *unsortedKeys = [self.alphabetizedItems allKeys];
        NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
        NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
        NSArray *fruitsForSection = [self.alphabetizedItems objectForKey:key];
        if (fruitsForSection.count == 1) {
            // Delete the whole section
            [self.alphabetizedItems removeObjectForKey:key];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        } else {
            // Delete the row from the data source
            NSInteger index = indexPath.row;
            //[self.items removeObjectAtIndex:index];
            [self.alphabetizedItems removeObjectForKey:indexPath];
            [tableView deleteRowsAtIndexPaths:@[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
    }
}
相关问题