iOS编辑表视图不起作用

时间:2013-10-26 04:13:24

标签: ios uitableview

我对此很新,我遇到了问题。我无法编辑我创建的表格视图...

我在应用程序右上角有一个按钮,上面写着“编辑”和- (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.

3 个答案:

答案 0 :(得分: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;
 }

答案 1 :(得分: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];
    }
}

希望这会对你有所帮助

答案 2 :(得分: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