2013-10-26 56 views
0

我對此很新,並且陷入了一個問題。我無法編輯我創建的表格視圖的功能...iOS編輯表格視圖不能正常工作

我在應用程序的右上角有一個按鈕,上面寫着「編輯」和- (IBAction)editTable:(id)sender,我已經嘗試過多次嘗試讓編輯這個名單我已經創建...

@implementation XYZViewController 
@synthesize animalNames; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //create array called animal names 
    animalNames = [[NSArray alloc]initWithObjects: 

          @"Cow", 
          @"Dog", 
          @"Cat", 
          @"Dear", 
          @"Penguin", 
          @"Lion", 
          @"Leapord", 
          @"Eal", 
          @"Snake", 
          @"Moth", 
          @"Cheetah", 
          @"Turtle" 

          , nil]; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [self.animalNames count]; 

} 

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

    NSString *identifier = @"MainCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 

    } 

    cell.textLabel.text = [self.animalNames objectAtIndex:indexPath.row]; 

    return cell; 

} 

- (IBAction)editTable:(id)sender 
{ 

    NSLog(@"Editing"); 

    [super setEditing:TRUE]; 
    [self.tableView setEditing:TRUE]; 
    self.editing = YES; 

} 



@end 

使用Xcode的5

回答

1

您需要實現以下方法來支持表格視圖編輯:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Return NO if you do not want the specified item to be editable. 
return YES; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return UITableViewCellEditingStyleDelete; 
} 


- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return NO; 
} 
0

要編輯你需要調用

[self.tableView setEditing:YES animated:YES];要在其上使用則

表的表視圖

你必須實現委託方法

tableView:commitEditingStyle:forRowAtIndexPath: 

方法,使行的刪除。爲了刪除您需要使用

然後行做..

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Remove the object at the index we need to delete 

    [YourTableArray removeObjectAtIndex:indexPath.row]; 


    //Delete the rows at the Indexpath. 
    [YourTable deleteRowsAtIndexPaths:[[NSArray alloc]initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight]; 
    [reminder_table reloadData]; 

    //Set the table to editing NO. 
    [YourTable setEditing:NO animated:YES]; 
} 

請看以下鏈接供你參考

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19

1

您應該在將要編輯它時將animalNames聲明爲NSMutableArray。然後你需要重寫一些委託方法來在tableview中執行編輯。

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ 
    NSString *sourceItem = _animalNames[sourceIndexPath.row]; 
    [_animalNames removeObjectAtIndex:sourceIndexPath.row]; 
    [_animalNames insertObject:sourceItem atIndex:destinationIndexPath.row]; 
} 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return YES; 
} 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (editingStyle == UITableViewCellEditingStyleDelete){ 
     [_animalNames removeObjectAtIndex:indexPath.row]; 
     [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

可以在editTable方法停止編輯像

- (IBAction)editTable:(id)sender 
    UIBarButtonItem *button = (UIBarButtonItem *)sender; 
    if ([button.title isEqualToString:@"Edit"]) { 
     button.title = @"Done"; 
     [self.tableView setEditing:TRUE]; 
    } else { 
     button.title = @"Edit"; 
     [self.tableView setEditing:NO]; 
    } 
} 

希望這將幫助你