2014-11-11 4 views
1

все. В настоящее время я пытаюсь понять, как добавить UIAlertView, который будет запрашивать у пользователя, хотят ли они удалить все элементы или только один из списка покупок.UIAlertView для удаления строки из контроллера UITableView

У меня возникли проблемы с этим, так как из всего, что я нашел, мне нужно использовать didDismissButtonWithIndex, чтобы выяснить, какая кнопка была нажата.

Любые идеи?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     for (myData *item in self.itemData) 
     { 
      if(item.count > 1) 
      { 
       UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"delete how many?" delegate:self cancelButtonTitle:@"1" otherButtonTitles:@"All", nil]; 
       [alert show]; 
      } 
      else 
      { 
       int index = indexPath.row; 
       [self.itemData removeObjectAtIndex:index]; 

       [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

      } 
     } 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex == 0) { 
     //selected item's count-- 
    } 
    if(buttonIndex == 1) { 
     //selected item deleted completely 
    } 
} 
+0

что именно ваша проблема? с помощью 'alertView: didDismissWithButtonIndex:' является прекрасным подходом :), если вы хотите быть немного понятнее, что означают индексы, вы можете объявить их как константы, чтобы дать им некоторую дополнительную семантику. – nburk

+0

ru нужен метод alert в методе didselectatrowatindexpath или любой кнопки метод действия –

+0

@nburk Я знаю, что didDismissWithButtonIndex: это правильный способ обойти это. Я просто не уверен, что я удалю элемент оттуда без tableView commiEditingStyle. – Josh

ответ

0

С iOS8 вы можете использовать UIAlertController

UIAlertController *myAlert = [UIAlertController alertControllerWithTitle:@"How many?" 
                    message:@"" 
                  preferredStyle:UIAlertControllerStyleAlert]; 

и определяют действия непосредственно в UIAlertAction блок

UIAlertAction *oneAction = [UIAlertAction actionWithTitle:@"One" 
                 style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 
              //define action 
}]; 

UIAlertAction *allAction = [UIAlertAction actionWithTitle:@"All" 
                 style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 
              //define action 
}]; 

Тогда вы бы просто добавить эти действия myAlert и отображения его.

[myAlert addAction:oneAction]; 
[myAlert addAction:allAction]; 

[self presentViewController:myAlert animated:YES completion:nil]; 
+0

Это было бы неплохо, но, к сожалению, я не пользуюсь IOS8 – Josh

Смежные вопросы