webschnecke
webschnecke

Reputation: 919

Delete selected row - not last saved row

App like a calender

I want to delete specific rows in my tableView. Each row is saved by a datepicker. The Title und Subtitle is perfekt. It shows the Name and the time selected in datePicker.

If I create more than one event, my tableView shows it in the correct order.

Also if I create the event in mixed intervals at first 11:10h than 11:00h But If I want to delete the Event Nr.1 it deletet Event Nr.2

It seems that my code deleted always the last saved event. Edit mode:

        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [[UIApplication sharedApplication] cancelLocalNotification:notifcation];
        [notificationsArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

        [tableview reloadData];
    }



}

Tableview:

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...


self.notificationsArray = [NSMutableArray arrayWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]];
notifcation = [self.notificationsArray objectAtIndex:indexPath.row];
//UILocalNotification *notif = [notificationsArray objectAtIndex:indexPath.row];
//NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
//UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];

//Was in der Zelle als Titel steht
//[[cell textLabel] setText:[notifcation alertBody]];
[[cell textLabel] setText:@"Event"];
//[cell.detailTextLabel setText:[notif.fireDate description]];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
[[cell detailTextLabel] setText:[dateFormatter stringFromDate:notifcation.fireDate]];
[dateFormatter release];
return cell;

}

Upvotes: 0

Views: 297

Answers (1)

Surjit Joshi
Surjit Joshi

Reputation: 3515

Add some lines in your method

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
                                            forRowAtIndexPath:(NSIndexPath *)indexPath
{

   if(tableView.editing && editingStyle == UITableViewCellEditingStyleDelete)
    {
            [tableView beginUpdates];
            [[UIApplication sharedApplication] cancelLocalNotification:notifcation];
                [notificationsArray removeObjectAtIndex:indexPath.row];

            // Animate the deletion from the table.
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                      withRowAnimation:UITableViewRowAnimationLeft];
            [tableView endUpdates];
            [tableView reloadData];
     }

}

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

Upvotes: 1

Related Questions