ios - 如果使用commitEdittingStyle为空,则删除tableview行和节

标签 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

最佳答案

基本方法是查看要删除的行的部分中有多少行。如果该部分有两行或更多行,请像现在一样简单地删除该行。如果该节只有一行(该行将被删除),则从数据模型中删除该节,然后从表中删除该节,而不是删除行。

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

关于ios - 如果使用commitEdittingStyle为空,则删除tableview行和节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26725819/

相关文章:

javascript - UA PhoneGap 2.0 插件在 iOS 上无法正确初始化

iphone - pdf 未添加到 IOS 5 中 Objective-C 中的电子邮件

ios - 我应该将 appDelegate 用于所有单例样式的应用程序级代码吗?

ios - 下载时播放音频而不下载文件两次?

objective-c - 在 iOS 类扩展中定义属性

objective-c - 如何用圆圈遮盖正方形图像并在图像周围放置黑色边框

objective-c - 可以在后台线程上创建 UIView 吗?

iphone - dequeueReusableCellWithIdentifier 使用 Storyboard静态单元格返回 nil

ios - 将图像从图像 URL 数组添加到 UITableViewCell

objective-c - 发送到实例自定义单元格的无法识别的选择器