UITableViewCell 中的自定义滑动功能不起作用

发布于 2024-10-06 19:05:51 字数 1728 浏览 4 评论 0原文

我需要在 uitableviewcell 中添加计数,这样当我触发滑动功能时,相应单元格中的计数应该增加,而点击时计数应该减少。

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *CellIdentifier = @"sample";
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    UISwipeGestureRecognizer *recognizer;

    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    [self addGestureRecognizer:recognizer];
    self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
    recognizer.delegate = self;
    [recognizer release];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];

    [self addGestureRecognizer:recognizer];
    [recognizer release];
    UILabel *cookieLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5, 120,30)];
    cookieLabel.text = @"hello";
    cookieLabel.font = [UIFont systemFontOfSize:15.0f];
    cookieLabel.textColor = [UIColor blackColor];
    cookieLabel.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:cookieLabel];
    [cookieLabel release];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    costLabel = [[UILabel alloc] initWithFrame:CGRectMake( 200, 5, 230, 30)];
    //costLabel.text = handleSwipeFrom:;
    costLabel.font = [UIFont systemFontOfSize:15.0f];
    costLabel.textColor = [UIColor blackColor];
    costLabel.backgroundColor = [UIColor greenColor];
    [cell.contentView addSubview:costLabel];
    [costLabel release];
    [self setUserInteractionEnabled:YES];

    return cell;
}

I need to add the count in the uitableviewcell in a such a way that when I trigger the swipe function the count should be incremented in the corresponding cell and while tapping the count should be decremented.

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *CellIdentifier = @"sample";
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    UISwipeGestureRecognizer *recognizer;

    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    [self addGestureRecognizer:recognizer];
    self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
    recognizer.delegate = self;
    [recognizer release];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];

    [self addGestureRecognizer:recognizer];
    [recognizer release];
    UILabel *cookieLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5, 120,30)];
    cookieLabel.text = @"hello";
    cookieLabel.font = [UIFont systemFontOfSize:15.0f];
    cookieLabel.textColor = [UIColor blackColor];
    cookieLabel.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:cookieLabel];
    [cookieLabel release];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    costLabel = [[UILabel alloc] initWithFrame:CGRectMake( 200, 5, 230, 30)];
    //costLabel.text = handleSwipeFrom:;
    costLabel.font = [UIFont systemFontOfSize:15.0f];
    costLabel.textColor = [UIColor blackColor];
    costLabel.backgroundColor = [UIColor greenColor];
    [cell.contentView addSubview:costLabel];
    [costLabel release];
    [self setUserInteractionEnabled:YES];

    return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-10-13 19:05:51

不要将 UISwipeGestureRecognizer 添加到单元格中。将其添加到 UITableView。

我使用 TISwipeableTableView 作为基础,并对其进行了大量修改以使其正常工作(他们做了自己的触摸处理,这导致了一种“奇怪的、非原生的”感觉)

- (void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
  if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
      CGPoint swipeLocation = [gestureRecognizer locationInView:self];
      NSIndexPath *swipedIndexPath = [self indexPathForRowAtPoint:swipeLocation];
      TISwipeableTableViewCell* swipedCell = (TISwipeableTableViewCell *)[self cellForRowAtIndexPath:swipedIndexPath];

      if ([swipedCell isKindOfClass:[TISwipeableTableViewCell class]]) {
        if (![swipedIndexPath isEqual:indexOfVisibleBackView]) {
          [self hideVisibleBackView:YES];
          [swipedCell revealBackView];
          [self setIndexOfVisibleBackView:swipedIndexPath];  

          if (swipeDelegate && [swipeDelegate respondsToSelector:@selector(tableView:didSwipeCellAtIndexPath:)]){
            [swipeDelegate tableView:self didSwipeCellAtIndexPath:[self indexPathForRowAtPoint:swipeLocation]];
          }        
        }
      }
    }  
  }
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
  if ((self = [super initWithFrame:frame style:style])) {
    if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
      UIGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)] autorelease];
      [self addGestureRecognizer:swipeGesture];
    }
  }
  return self;
}

这应该可以帮助您开始。

Don't add the UISwipeGestureRecognizer to the cell. Add it to the UITableView.

I used TISwipeableTableView as a base and modified it heavily to work correctly (they did their own touch handling, which resulted in a "weird, unnative" feeling)

- (void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
  if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
      CGPoint swipeLocation = [gestureRecognizer locationInView:self];
      NSIndexPath *swipedIndexPath = [self indexPathForRowAtPoint:swipeLocation];
      TISwipeableTableViewCell* swipedCell = (TISwipeableTableViewCell *)[self cellForRowAtIndexPath:swipedIndexPath];

      if ([swipedCell isKindOfClass:[TISwipeableTableViewCell class]]) {
        if (![swipedIndexPath isEqual:indexOfVisibleBackView]) {
          [self hideVisibleBackView:YES];
          [swipedCell revealBackView];
          [self setIndexOfVisibleBackView:swipedIndexPath];  

          if (swipeDelegate && [swipeDelegate respondsToSelector:@selector(tableView:didSwipeCellAtIndexPath:)]){
            [swipeDelegate tableView:self didSwipeCellAtIndexPath:[self indexPathForRowAtPoint:swipeLocation]];
          }        
        }
      }
    }  
  }
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
  if ((self = [super initWithFrame:frame style:style])) {
    if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
      UIGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)] autorelease];
      [self addGestureRecognizer:swipeGesture];
    }
  }
  return self;
}

This should get you started.

半衾梦 2024-10-13 19:05:51

[单元格添加GestureRecognizer:识别器]

[cell addGestureRecognizer:recognizer]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文