2012-01-30 18 views

cevap

6

Bunu denemedim ve uygulamam ama bir çekim yapacağım. Birincisi, özel bir UITableViewCell oluşturmak ve bunu, ona kullanılıyor tableView için

  1. Bir referans kullanabilirsiniz 2 özelliklere sahip olsun.
  2. Bir indexPath tableView hücreyi bulmak için. (Not, Bu, tüm hücreler için bir hücreyi sildiğiniz her seferinde güncellenmelidir.

Özel hücreyi oluşturduğunuz cellForRowAtIndexPath:, bu özellikleri ayarlayın. Ayrıca

cell.tableView=tableView; 
cell.indexPath=indexPath; 
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)]; 
[cell addGestureRecognizer:swipeGestureRecognizer]; 
[swipeGestureRecognizer release]; 

hareket sadece yatay defa hızlıca kaydırarak aldığı emin olun hücreye bir UISwipeGestureRecognizer ekleyin. @MadhavanRP tarafından gönderildi sizin deleteCell:

-(void) deleteCell:(UIGestureRecognizer*)gestureRec 
{ 
    UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec; 
    CustomCell *cell=[swipeGestureRecognizer view]; 
    UITableView *tableView=cell.tableView; 
    NSIndexPath *indexPath=cell.indexPath; 
    //you can now use these two to perform delete operation 
} 
+0

Hücreye eklemek için ilk başlatırken, hareket algılayıcısının yönünü ayarlamanız gerektiğini unutmayın. Böyle bir şey: swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – ColossalChris

+0

Apple, '(void) cancelPreviousPerformRequestsWithTarget: (id) aTarget seçici: (SEL) aSelector' sağlamadığınız için sizi lanetliyor. –

-2

.

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

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 
    }  
} 

Else mor bilgi u enter link description here

+0

Vivek2012 ve iWill - Yanıtlarınız için teşekkürler, ancak DELETE düğmesini göstermek istemiyorum, stackoverflow'taki yanıtlar zaten var. Hücrenin, hızlıca sola veya sağa doğru hareket ettirdiği anda silinmesini istiyorum.lütfen açıklamamı okuyun – carbonr

-1

ile hücreyi silmek için sth UITableViewCellEditingStyle UITableViewCellEditingStyleDelete ise o zaman,

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

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

bu iki yöntemleri uygulamak yapmak bu bağlantı üzerinden gidebilir

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 

hepsi bu.

3

çözümde

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&& 
     ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft 
     ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES; 
} 

çalışır, ancak olması gerekenden daha karmaşıktır. Daha basit bir yaklaşım benimseyebilir ve masadaki tüm swipes'ları işleyen bir tane jest tanıyıcı oluşturabilir ve daha sonra kullanıcının hangi hücreyi geçirdiğini belirlemek için kaydırmanın yerini alabilirsiniz. ayarlamak için

jest tanıyıcı:

- (void)setUpLeftSwipe { 
    UISwipeGestureRecognizer *recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                 action:@selector(swipeLeft:)]; 
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.tableView addGestureRecognizer:recognizer]; 
    recognizer.delegate = self; 
} 

Çağrı tokatlamak işlemek için viewDidLoad

bu yöntem:

- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { 
    CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
    ... do something with cell now that i have the indexpath, maybe save the world? ... 
} 

not: sizin vc jest tanıyıcı temsilci uygulamalıdır .

İlgili konular