我想从左侧向左滑动时删除所有选定的单元格。
我的.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];
}
}
我希望在按下调用deleteCurrentObject方法的按钮时删除所有swipedCell.selected。我该怎么做?
答案 0 :(得分:1)
将下面的内容放在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"];
答案 1 :(得分: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];
}
}