简体   繁体   中英

Subclassing the UITableViewCell and adding own functionalities in iOS

How to add a gesture(left to right) which will call

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

method to a UITableViewCell by subclassing it in iOS7.

If you have NSMutableArray* sourceArray as dataSource for your tableView, try something like this:

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete)
    {
        [sourceArray removeObjectAtIndex:indexPath.row];

        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView endUpdates];
    }
}

You should create custom class delivered from UITableViewCell and you should add UIPanGestureRecognizer in init:

UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];

The next step is override method gestureRecognizerShouldBegin:

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer translationInView:[self superview]];
    if (fabsf(translation.x) > fabsf(translation.y)) {
        return YES;
    }
    return NO;
}

and add handlePan:

    -(void)handlePan:(UIPanGestureRecognizer *)recognizer {   
        if (recognizer.state == UIGestureRecognizerStateBegan) {
            _originalCenter = self.center; //variable to keep centre
        }

        if (recognizer.state == UIGestureRecognizerStateChanged) {
            CGPoint translation = [recognizer translationInView:self];
            //this check out if you drag more than half of the screen width
            self.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
            _deleteOnDragRelease = self.frame.origin.x < -self.frame.size.width / 2;
        }

        if (recognizer.state == UIGestureRecognizerStateEnded) {
            CGRect originalFrame = CGRectMake(0, self.frame.origin.y,
                                              self.bounds.size.width, self.bounds.size.height);
            if (!_deleteOnDragRelease) {
                [UIView animateWithDuration:0.2
                                 animations:^{
                                     //this is for animate that you move the cell but you don't need it (it just look cool)
                                     self.frame = originalFrame;
                                 }
                 ];
            }
if (_deleteOnDragRelease) {
            // need to implement your delegate
            [self.delegate itemToDeleted:self.YOURDATA];
        }
        }
    }

You need to add protocol with method itemToDeleted: and you need to implement it in tableView:commitEditingStyle. Let me know if you need more help with that. Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM