ios - 继承 UITableViewCell 并在 iOS 中添加自己的功能

标签 ios objective-c uitableview ios7

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

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

在 iOS7 中通过子类化 UITableViewCell 的方法。

最佳答案

您应该创建从 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 添加协议(protocol):并且需要在 tableView:commitEditingStyle 中实现它。如果您需要更多帮助,请告诉我。 希望这有帮助

关于ios - 继承 UITableViewCell 并在 iOS 中添加自己的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212696/

相关文章:

ios - 自定义 CALayer 子类未显示

javascript - iOS Safari上的YouTube API,使用自定义控件以编程方式静音/取消静音?

iOS - 键盘建议气泡错误地隐藏在键盘后面

iphone - 在类方法中访问实例变量,Objective C

ios - 来自轻弹 Action 和 UIPanGestureRecognizer 的意外结果

ios - 为什么表格中的第一节标题未显示?

iphone - AFNetworking + JSONKit 不能一起工作

ios - 离线地址获取

ios - 表格 View 单元格加载动画一个接一个

ios - 当 6 出现在 UITableView 时,如何重用这些数字背景颜色 1-5?