2017-06-13 67 views
0

Using this tutorial,我已經能夠覆蓋我的表編輯,以允許按editButtonItem來切換「添加行」按鈕。不過,我也希望它不允許滑動刪除,除非打開編輯 - 但滑動刪除始終處於啓用狀態。按editButtonItem不啓用/禁用刪除滑動

我已經看到了其他堆棧溢出問題,但它看起來像我已經實現了其他用戶建議需要實現的一切。我肯定錯過了什麼。

以下是我的表編輯代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    tableData = ptrBookmarks; 

    numberOfSections = 1; // for editing: initial number of sections 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 

    //allow row selection during editing. 
    //if the "Add Row" row is selected we can trigger an insert. 
    //rather than forcing the using to click the (+) icon directly 
    self.aTableView.allowsSelectionDuringEditing = YES; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 

    if ([self isEditing]) // the current view is in editing mode, return count + an extra row 
     return [tableData count] + 1; 
    else // return count 
     return [tableData count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 

    //if number of rows is greater than the total number of rows in the data set 
    //and this view is in editing mode. 
    //Initialize the cell for "Add Row" 
    //there will be an extra row once SetEditing: is called 
    if(indexPath.row >= ptrBookmarks1.count && [self isEditing]){ 
     cell.textLabel.text = @"Add Row"; 
    } 
    else { 
     cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 


//VIEW CONTROLLER METHOD: IMPORTANT 
//this is a method of the view controller 
//if we use apple's editing button as follows: 
//self.navigationItem.rightBarButtonItem = self.editButtonItem 
//then this method will be called automatically for us. 
//if we are using a button callback or similar method, 
//then we need to call setEditing: manually on the view 
-(void) setEditing:(BOOL)editing animated:(BOOL)animated{ 

    //wrap our code in an if statement 
    //only run the code if we are not swipe deleting a row. 
    //if we were called due to a swipeDelete action, ignore it 
    if(isSwipeDeleting == NO){ 
     //call parent 
     [super setEditing:editing animated:animated]; 


     //if editing mode 
     if(editing){ 
      //batch the table view changes so that everything happens at once 
      [self.aTableView beginUpdates]; 
      //for each section, insert a row at the end of the table 
      for(int i = 0; i < numberOfSections; i++){ 
       //create an index path for the new row 
       NSIndexPath *path = [NSIndexPath indexPathForRow:tableData.count inSection:i]; 
       //insert the NSIndexPath to create a new row. NOTE: this method takes an array of paths 
       [self.aTableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      } 
      //animate the changes now 
      [self.aTableView endUpdates]; 
     }else{ 
      //batch the table view changes so that everything happens at once 
      [self.aTableView beginUpdates]; 
      //for each section, insert a row at the end of the table 
      for(int i = 0; i < numberOfSections; i++){ 
       //create an index path for the new row 
       NSIndexPath *path = [NSIndexPath indexPathForRow:tableData.count inSection:i]; 
       //insert the NSIndexPath to create a new row. NOTE: this method takes an array of paths 
       [self.aTableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      } 
      //animate the changes now 
      [self.aTableView endUpdates]; 
     } 
    } 
} 


//DELEGATE METHOD: 
//this method will be called for every row and allows us to set the 
//editing syle icon(Delete,Insert) 
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    // Detemine if it's in editing mode 
    //if (self.aTableView.editing) { 
    //use the + icon(add icon) on row 
    //if this is the additional row created in setEditing:animated: 
    if(indexPath.row >= tableData.count){ 
     return UITableViewCellEditingStyleInsert; 
    } 
    else{ 
     //use the delete icon on this row 
     return UITableViewCellEditingStyleDelete; 
    } 
    //} 
    //else 
    //return UITableViewCellEditingStyleNone; 
} 

//handle the deletion insertion 
//this method is called when the delete or insert icon has been press. 
//we should update our dataSource by inserting or removing the selected INDEX 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //remove row from datasource 
     [tableData removeObjectAtIndex:indexPath.row]; 
     //remove the row in the tableView because the deleteIcon was clicked 
     [self.aTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     //add a new row to the datasource 
     [tableData addObject:@"New Icon"]; 
     //insert a row in the tableView because the plusIcon was clicked. 
     [self.aTableView insertRowsAtIndexPaths:@[indexPath] 
           withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
} 


//if we are in editing mode we do not want to perform Seque Transition 
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { 
    if ([identifier isEqualToString:@"MyDetailView"] && [self isEditing]) { 
     return NO; 
    } 
    return YES; 
} 


//this method is called when the user swipes to delete a row. 
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{ 
    isSwipeDeleting = YES;//user just swipe to delete a row 
} 
//when the user cancel the swipe or click the delete button 
//this method is call 
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{ 
    isSwipeDeleting = NO;//swipe to delete ended. No longer showing the DELETE button in cell 
} 



// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 

    NSString *item; 

    item = [[tableData objectAtIndex:fromIndexPath.row] retain]; 
    [tableData removeObjectAtIndex:fromIndexPath.row]; 
    [tableData insertObject:item atIndex:toIndexPath.row]; 

    [item release]; 
} 




// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 

#pragma mark - Table view delegate 

//DELEGATE METHOD: 
//the user selected a row 
//In order for the user to perform an INSERTION action on a row, 
//they have to click the + icon icon. We can implement this method 
//so that they can click anywhere on the add row to add a new row 
//tableView.allowsSelectionDuringEditing = YES; must be set 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    //deselect the selected row with animatiion 
    [self.aTableView deselectRowAtIndexPath:indexPath animated:YES]; 

    //if the selected row was the "Add Row" row call tableView:commitEditingStyle: 
    //to add a new row 
    if (indexPath.row >= tableData.count && [self isEditing]) { 
     [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:indexPath]; 
    } 
    else { // otherwise do regular table item selection 
     [self.delegate didTapBookmarksTable:[tableData objectAtIndex:indexPath.row]]; 
    } 

} 

回答

0

發現的問題。我將editingStyleForRowAtIndexPath方法中的主要if語句更改爲self.editing而不是self.aTableView.editing

// Detemine if it's in editing mode 
    if (self.editing) { 
     //use the + icon(add icon) on row 
     //if this is the additional row created in setEditing:animated: 
     if(indexPath.row >= tableData.count){ 
      return UITableViewCellEditingStyleInsert; 
     } 
     else { 
      //use the delete icon on this row 
      return UITableViewCellEditingStyleDelete; 
     } 
    } 
    else 
     return UITableViewCellEditingStyleNone;