2014-11-04 55 views
1

我似乎無法找到太多這一點。我目前正在嘗試創建一個可滑動的刪除按鈕,該按鈕將刪除被滑動的行,並且如果該行現在從節頭中爲空,它也將刪除節頭。例如,「麪包」被刪除,並且節頭「B」下沒有其他內容。然後這將刪除麪包和「B」部分標題。我的代碼如下。刪除tableview的行和部分,如果與commitEdittingStyle空的

@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 
+0

僅供參考 - 您的代碼非常低效。 'numberOfRowsInSection'和'cellForRowAtIndexPath'將被調用很多次。你一直在重複排序鍵。將它們排序一次並保留參考。 – rmaddy 2014-11-04 00:37:20

+0

我其實有點新鮮。所以,我從教程中獲得了代碼,但它使用NSArrays和NSDictionaries。我想象,因爲他們不可變,這不是一個大問題。我想知道你是怎麼說我應該解決這個問題的。我基本上只是爲fruitForSection做一個屬性NSArray,並做numberdfRowsInSection裏面的viewdidload 4行,返回[self.fruitsForSection count];? – Josh 2014-11-04 01:09:36

+0

沒有必要一遍又一遍地對數據進行排序。當您首次填充'self.alphabetizedItems'時,將其排序一次。將排序的鍵保存在另一個實例變量中。 – rmaddy 2014-11-04 01:11:28

回答

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 
    } 
} 
+0

真的很有幫助。非常感謝你。 – Josh 2014-11-04 01:07:56