Удалить выбранную строку - не последнюю сохраненную строку - PullRequest
0 голосов
/ 17 ноября 2011

Приложение как календарь

Я хочу удалить определенные строки в моем tableView. Каждая строка сохраняется датером. Название и Субтитры - это совершенство. Он показывает имя и время, выбранное в DatePicker.

Если я создаю более одного события, мой tableView показывает его в правильном порядке.

  • Событие №1 - 11:00 ч.
  • Событие 2 - 11:10 ч.
  • и т. Д.

Также, если я создаю событие в смешанных интервалах вначале 11: 10h, а не 11:00 Но если я хочу удалить Событие №1, то удаляю Событие №2

.

Кажется, мой код всегда удалял последнее сохраненное событие. Режим редактирования:

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

}

1 Ответ

1 голос
/ 17 ноября 2011

Добавьте несколько строк в ваш метод

- (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;
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...