繁体   English   中英

对UITableViewCell进行子类化并在iOS中添加自己的功能

[英]Subclassing the UITableViewCell and adding own functionalities in iOS

如何添加将调用的手势(从左到右)

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

通过在iOS7中对其进行子类化来实现UITableViewCell的方法。

如果您的tableView有NSMutableArray* sourceArray作为dataSource,请尝试以下方法:

- (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];
    }
}

您应该创建从UITableViewCell提供的自定义类,您应该在init中添加UIPanGestureRecognizer:

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

下一步是覆盖方法gestureRecognizerShouldBegin:

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

并添加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];
        }
        }
    }

您需要使用方法itemToDeleted添加协议:您需要在tableView:commitEditingStyle中实现它。 如果您需要更多帮助,请告诉我。 希望这有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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