ios刷uitableviewcell删除有时工作

时间:2014-03-20 07:21:37

标签: ios uitableview swipe-gesture

我有UIViewController,其子视图为UITableView。我想这样做,以便可以通过向左滑动单元格然后点击UITableViewCells来删除Delete

但是,只是偶尔会抓住滑动。它有时很好用,但大部分时间都没有抓住滑动,并且delete按钮没有出现。这是我正在使用的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Return YES if you want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)table editingStyleForRowAtIndexPath:    (NSIndexPath *)indexPath
{
   return UITableViewCellEditingStyleDelete;
}

// Swipe to delete.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // perform delete logic
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Card *thisCard = self.cards[indexPath.row];
    static NSString *CellIdentifier = @"CardCell";
    CardCell *cell = (DCTStripeCardCell *)[DCTBase cellForTableView:tableView reuseID:CellIdentifier indexPath:indexPath];

    BOOL first = indexPath.row == 0;
    BOOL last = indexPath.row == ([self.cards count] - 1);
    BOOL isDefault = thisCard.resourceId == self.customer.defaultCard.resourceId;
    [cell update:thisCard First:first Last:last Default:isDefault];
    if (isDefault) {
        self.selectedCell = cell;
    }
    return cell;
}

以下是[cell update:First:Last:Default]

的代码
- (void)update:(Card *)card First:(BOOL)first Last:(BOOL)last Default:(BOOL)isDefault
{
    [super updateFirst:first Last:last];
    self.last4.text = [NSString stringWithFormat:@"CARD ENDING IN: %@", card.last4];
    self.expiration.text = [NSString stringWithFormat:@"EXPIRES: %@/%@", card.expMonth, card.expYear];
    self.cc_logo.image = [UIImage imageNamed:[card getCardImageString]];

    self.defaultCardView.clipsToBounds = YES;
    self.defaultCardView.layer.cornerRadius = self.defaultCardView.frame.size.height/2.0;
    self.defaultCardView.layer.borderWidth = 1.0f;
    if (isDefault) {
        self.defaultCardView.backgroundColor = [UIColor blueColor];
    }
}

我使用[super update:First:Last]来舍入第一个和最后一个表格单元格,因为ios 7不再支持此功能。

以下是代码:

- (void)updateFirst:(BOOL)first Last:(BOOL)last
{

    self.clipsToBounds = YES;
    if (first) {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:  (UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(7.0, 7.0)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }

    // TODO: move common logic to another function.
    if (last) {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(7.0, 7.0)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }

    // This code is probably extraneous
    if (!first && !last) {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:  (UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(0.0, 0.0)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}

不确定我在这里缺少什么。我应该注意到,我还在实施delegate方法tableView didSelectRowAtIndexPath:

我已在simulator以及device上对此进行了测试,因此不仅仅是模拟器缺少滑动。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我犯了一个愚蠢的错误 - 没有意识到这个特定的UIViewController是从已添加UIGestureRecognizer的类继承而来的。为了解决这个问题,我只是在父类中实现了这个函数:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}