2012-02-27 21 views
27

Mam tablicy:Swipe Usuwanie Tableview Row

self.colorNames = [[NSArray alloc] 
initWithObjects:@"Red", @"Green", 
@"Blue", @"Indigo", @"Violet", nil]; 

Próbowałem wszystkiego mogę znaleźć, ale zawsze błędy. Chcę móc przesuwać palcem, aby pojawił się przycisk usuwania, a następnie nacisnąć przycisk usuwania i usunąć ten wiersz.

Pełny kod (wszystko związane z tabeli):

// HEADER FILE 
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 

    IBOutlet UITableView *tableView; 
    NSMutableArray *colorNames; 

} 
@property (strong, nonatomic) NSArray *colorNames; 


@end 

// IMPLEMENTATION 

#import "ViewController.h" 

@implementation ViewController 

@synthesize colorNames; 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    self.colorNames = [[NSMutableArray alloc] 
    initWithObjects:@"Red", @"Green", 
    @"Blue", @"Indigo", @"Violet", nil]; 

    [super viewDidLoad]; 
    //[tableView setEditing:YES animated:NO]; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    int count = [colorNames count]; 
    return count; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier]; 

} 
    // Configure the cell. 
    cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]]; 

return cell; 

} 
+0

pan tylko pisał tu o 'NSArray'. Opublikuj odpowiedni kod dotyczący konfiguracji 'UITableView'. – PengOne

+0

Jest tam. Proszę zaoferować wszelkie możliwe dane wejściowe, ponieważ doceniam każdą pomoc. – JTApps

Odpowiedz

121

Trzeba wdrożyć niezbędne UITableViewDelegate i UITableViewDataSource metod.

pierwsze, dodać to:

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

Następnie:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //remove the deleted object from your data source. 
     //If your data source is an NSMutableArray, do this 
     [self.dataArray removeObjectAtIndex:indexPath.row]; 
     [tableView reloadData]; // tell table to refresh now 
    } 
} 
+0

Zaszedłem tak daleko, ale muszę wiedzieć, co właściwie włożyć w te metody. Zrobiłem to już kilkanaście razy, ale zawsze dochodzę do jakiejś bariery z powodu przestarzałych samouczków. – JTApps

+0

Metody te nie zmieniły się bardzo od czasu uruchomienia systemu iOS. Przestarzałe samouczki powinny działać poprawnie. – edc1591

+0

Zaktualizowałem moją odpowiedź. – edc1591

4

Wdrożenie następującą metodę:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 
+0

Już to dodałem. To, co muszę zrobić, to zezwolić na usunięcie. – JTApps

+0

Jeśli chcesz tylko usunąć, wydaje się, że nie musisz implementować tej metody, ponieważ domyślnym zachowaniem jest przycisk "Usuń". –

21

Na początek, twój colorNames powinny być NSMutableArray raczej niż NSArray. Nie można dodawać ani usuwać obiektów z regularnej (niezmiennej) tablicy; musisz go odtworzyć za każdym razem, gdy dokonasz zmiany. Zmiana, która to ułatwi. Dla Państwa wykonania -tableView:commitEditingStyle:forRowAtIndexPath:, będziemy wtedy w stanie zrobić coś takiego:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [colorNames removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
    } 
} 
+1

Zmieniająca się część tablicy pomogła, nie wiem, dlaczego wcześniej jej nie złapałem! – JTApps

+2

+1 dla [tableView deleteRowsAtIndexPaths], aby aktualizacja interfejsu użytkownika, jak również źródło danych –

0

To co pracował dla mnie

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

    if (editingStyle == UITableViewCellEditingStyleDelete){ 
     Comment *comment = [self.commentArray objectAtIndex:indexPath.row]; 
     dispatch_async(kBgQueue, ^{ 
      if([self.thePost deleteCommentForCommentId:comment.commentId]){ 
       if (indexPath.row < self.commentArray.count) { 
        [self.commentArray removeObjectAtIndex:indexPath.row]; 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 

        [self.tvComments reloadData]; 
       }); 
      } 
     }); 
    } 
    [self.tvComments reloadData]; 
} 
0

Swift Wersja:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.Delete { 
     dataArray?.removeAtIndex(indexPath.row) 
     tableview.reloadData() 
    } 
} 
3

Odpowiedzi Swift 3 i Swift 4 bez użycia reloadData

Wolę używać deleteRows zamiast używania reloadData.

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    // Enables editing only for the selected table view, if you have multiple table views 
    return tableView == yourTableView 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     // Deleting new item in the table 
     yourTableView.beginUpdates() 
     yourDataArray.remove(at: indexPath.row) 
     yourTableView.deleteRows(at: [indexPath], with: .automatic) 
     yourTableView.endUpdates() 
    } 
} 
+1

deleteRows już obsługuje tableView aktualizacje dla ciebie. Możesz pominąć begin/endUpdates. – Rob

0

Swift 4 Wersja:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let delete = UITableViewRowAction(style: .destructive, title: "delete") { (action, indexPath) in 
     // delete item at indexPath 

    } 
    return [delete] 
}