2015-10-20 25 views
8

Przeszukuję kilka artykułów, ale nie znalazłem tego, czego szukam. Zasadniczo chcę wyświetlić przycisk Usuń w każdym wierszu, ale nie chcę używać właściwości UITableView.editing. Ponieważ wygląda to tak;UITableViewCell, pokaż przycisk usuwania stylu machnięcia bez przesunięcia

enter image description here

Istnieje przycisk "Edit" będzie. Kiedy użytkownik go kliknie, przycisk usuwania będzie wyglądał jak przesunięcie.

Czy jest szansa, aby pokazać przyciski usuwania takie jak to;

enter image description here

Może jest jakiś sposób, aby sobie z tym poradzić. W przeciwnym razie zamierzam stworzyć dla tego niestandardowy widok.

Dzięki za porady.

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

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

     //Do something... 
    } 
} 
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
      editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return UITableViewCellEditingStyleDelete; 

} 
+0

zrobić u chcą kasowania jak Image2 w tableview? –

+0

@ bhavinramani tak Chcę usunąć przyciski będzie wyglądać jak obraz2. Pojawi się przycisk "Edytuj", a po kliknięciu wszystkie wiersze powinny wyglądać jak obraz2. –

+0

Niestandardowa komórka tabeli to jedyny sposób, aby przejść do ciebie, ponieważ przycisk usuwania na końcu komórki nie byłby widoczny dla wszystkich komórek w widoku stołowym naraz (jak zostało to rozwinięte na ekranie) – viral

Odpowiedz

2
  • dodać obiekt logiczną: @property BOOL didPressEdit;
  • Dodaj UIButton do UITableViewCell
  • Po naciśnięciu edytować didPressEdit staje TRUE i UITableView załadunków, tak że cell.deleteButton.hidden = !didPressEdit; który sprawia, że ​​wszystkie Usuń przycisków dostępnych
  • Po naciśnięciu usuń, usuń obiekt z tablicy źródeł danych, załaduj ponownie widok tabeli

Mam nadzieję, że to pomoże

0

Możesz użyć metody delegata UITableView, aby poprosić o te działania. Zastosuj tę metodę w następujący sposób:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewRowAction *modifyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { 
    // Respond to the action. 
    }]; 
    modifyAction.backgroundColor = [UIColor blueColor]; 
    return @[modifyAction]; 
} 

Możesz oczywiście zwrócić wiele akcji i dostosować kolor tekstu i tła.

Implementacja tej metody jest również wymagane, aby wiersz edycji:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      [self.objects removeObjectAtIndex:indexPath.row]; 
      [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. 
     } 
    } 

Trzeba wywołać metodę editActionsForRowAtIndexPath na przycisk Edit kliknięcia.

-(void)buttonTouched:(id)sender{ 

     UIButton *btn = (UIButton *)sender; 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0]; 
     [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath]; 
    } 

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

Nie jestem pewien zrozumiałeś to pytanie. OP ma specyficzną potrzebę udostępnienia przycisku usuwania bez przesuwania i dla wszystkich wierszy w widoku tabeli po dotknięciu przycisku edycji. Nie widzę żadnego z osiągniętych dzięki udzielonej przez ciebie odpowiedzi. – viral

2
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewRowAction *moreAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     [self.heartCartTabel setEditing:NO]; 
     [self editButtonClicked:indexPath.row]; 
    }]; 
    moreAction2.backgroundColor = [UIColor blueColor]; 

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 

     [self tableView:self.heartCartTabel commitEditingStyle: UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath]; 
     //  [self.heartCartTabel deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    }]; 

    return @[deleteAction, moreAction2]; 
} 

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return YES; 
} 

- (IBAction)editButtonClicked:(int)indexNumber { 

}