Reeba Tahir
Reeba Tahir

Reputation: 13

iOS edit table view not working

I'm fairly new to this and am stuck on a problem. I can't get the ability to edit a table view i've created...

I have a button on the top right corner of the app that says "Edit" and - (IBAction)editTable:(id)sender, I've tried numerous attempts to get to edit this list I've created...

@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

Using Xcode 5.

Upvotes: 0

Views: 4247

Answers (3)

PKN
PKN

Reputation: 146

You should declare animalNames as NSMutableArray as you are going edit it. Then you need to override some delegate methods to perform edit in 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];
    }
}

You can stop editing in editTable method like

- (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];
    }
}

Hope this will help you

Upvotes: 1

creative_rd
creative_rd

Reputation: 695

To edit the table view you need to call

[self.tableView setEditing:YES animated:YES]; on the table which you are using then

You have to implement the delegate method

tableView:commitEditingStyle:forRowAtIndexPath: 

method to enable the deleting of rows. In order to delete the rows you need to use

Then Do this..

-(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];
}

Please find below link for your reference

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

Upvotes: 0

Abhinav
Abhinav

Reputation: 38142

You need to implement following methods to support table view editing:

- (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;
 }

Upvotes: 2

Related Questions