2014-01-28 57 views
0

我想從我向左滑動時刪除所有選定的單元格。從UITableView中刪除選定的單元格

我.m文件

- (IBAction)handleSwipeLeft:(UISwipeGestureRecognizer *)sender{ 

    CGPoint location = [_gestureSwipeLeft locationInView:_tblThingsToDo]; 
    NSIndexPath *swipedIndexPath = [_tblThingsToDo indexPathForRowAtPoint:location]; 
    UITableViewCell *swipedCell = [_tblThingsToDo cellForRowAtIndexPath:swipedIndexPath]; 

    if (swipedCell.selected == NO) { 
     swipedCell.selected = YES; 
    } 
    else{ 
    swipedCell.selected = NO; 
    } 
} 


-(void)deleteCurrentobject:(id)sender{ 
    if(self.selectedCells){ 
     [_things removeObjectAtIndex:_indexPath.row]; 
     [self.tblThingsToDo deleteRowsAtIndexPaths:[NSArray arrayWithObject:_indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

我希望所有的swipedCell.selected要刪除的調用deleteCurrentObject方法按鈕按下時。我該怎麼做?

+0

添加swipedCell.selected財產KVC下,並設置目標方法作爲KVC方法上下文中的選擇器(即deleteCurrentobject :)。 – Tirth

+0

我該怎麼做?什麼是KVC? – Dridia

+0

KVC意思是鍵值編碼。通過這個文檔https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVOCompliance.html#//apple_ref/doc/uid/20002178-BAJEAIEE – Tirth

回答

0

試試這個,下面行的東西

- (IBAction)handleSwipeLeft:(UISwipeGestureRecognizer *)sender{ 

    CGPoint location = [_gestureSwipeLeft locationInView:_tblThingsToDo]; 
    NSIndexPath *swipedIndexPath = [_tblThingsToDo indexPathForRowAtPoint:location]; 
    _indexPath = swipedIndexPath; 
    UITableViewCell *swipedCell = [_tblThingsToDo cellForRowAtIndexPath:swipedIndexPath]; 

    if (swipedCell.selected == NO) { 
     swipedCell.selected = YES; 
    } else{ 
     swipedCell.selected = NO; 
    } 
} 


-(void)deleteCurrentobject:(id)sender{ 
    if(self.selectedCells){ 
     [_things removeObjectAtIndex:_indexPath.row]; 
     [self.tblThingsToDo beginUpdates]; 
     [self.tblThingsToDo deleteRowsAtIndexPaths:[NSArray arrayWithObject:_indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.tblThingsToDo endUpdates]; 
    } 
} 
+0

我似乎不喜歡它的工作。 – Dridia

+0

@ user3100927你的代碼什麼是'self.selectedCells'? – Akhilrajtr

1

廣場swipedCell類否則swipedCell創建對象的時候init方法裏面。

[self addObserver: swipedCell forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:NULL]; 

當你handleSwipeLeft:方法調用時,它將自動調用下面的方法,

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ 
    if ([keyPath isEqualToString:@"selected"] && [object isKindOfClass:[UITableViewCell class]]) { 
     if(object.selected){ 
     [_things removeObjectAtIndex:_indexPath.row]; 
     [self.tblThingsToDo beginUpdates]; 
     [self.tblThingsToDo deleteRowsAtIndexPaths:[NSArray arrayWithObject:_indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.tblThingsToDo endUpdates]; 
     } 
    } 
} 

在下面的代碼的東西刪除觀察者使用

[self removeObserver:swipedCell forKeyPath:@"selected"]; 
相關問題