2013-10-26 59 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]; 
    } 
} 

希望这将帮助你