0

我对此很陌生,并且遇到了一个问题。我无法编辑我创建的表格视图...

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

4

3 回答 3

2

您需要实现以下方法来支持表格视图编辑:

- (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;
 }
于 2013-10-26T04:19:11.307 回答
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];
    }
}

希望对你有帮助

于 2013-10-26T05:07:25.810 回答
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

于 2013-10-26T04:18:06.810 回答