当我uitableviewcell被滑动时调用didSelectRowAtIndexPath

时间:2015-04-23 11:28:35

标签: ios objective-c iphone uitableview didselectrowatindexpath

我在我的申请中使用ABMenuTableViewCell tableview controller。 我想在刷一个单元格时调用didSelectRowAtIndexPath

现在didSelectRowAtIndexPath仅在我点击一个单元格时执行,即使我轻扫它也要调用它。这是我的didSelectRowAtIndexPathcellforRowAtIndexPath方法代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        UILabel *likes;
        UILabel *downloads;
        static NSString *CellIdentifier = @"Cell";
        ABMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell = [[ABMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"acc_arrow_back.png"]];
            arrow.frame = CGRectMake(300, 50, 5, 12);
            arrow.image = [arrow.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            [arrow setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
            [cell.contentView addSubview:arrow];

UIImageView *likes_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"social.png"]];
            likes_img.frame = CGRectMake(15, 80, 15, 15);
            likes_img.image = [likes_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            [likes_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
            [cell.contentView addSubview:likes_img];

            likes =[[UILabel alloc]initWithFrame:CGRectMake(33, 78, 80, 20)];
            likes.tag = 1001;    // set a tag for this View so you can get at it later
            likes.textColor=[UIColor darkGrayColor];
            likes.font=[UIFont fontWithName:@"Helvetica" size:10.0f];
            likes.text=[[rssOutputData objectAtIndex:indexPath.row]xmllikes];
            [cell.contentView addSubview:likes];
            cell.detailTextLabel.textColor = [UIColor darkGrayColor];

            UIImageView *downloads_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"download.png"]];
            downloads_img.frame = CGRectMake(55, 79, 15, 15);
            downloads_img.image = [downloads_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            [downloads_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
            [cell.contentView addSubview:downloads_img];

            downloads =[[UILabel alloc]initWithFrame:CGRectMake(73, 78, 80, 20)];
            downloads.tag = 1002;    // set a tag for this View so you can get at it later
            downloads.textColor=[UIColor darkGrayColor];
            downloads.font=[UIFont fontWithName:@"Helvetica" size:10.0f];
            downloads.text=[[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
            [cell.contentView addSubview:downloads];
            cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        }
        else
        {
            // use viewWithTag to find lblNombre in the re-usable cell.contentView
            likes = (UILabel *)[cell.contentView viewWithTag:1001];
            downloads = (UILabel *)[cell.contentView viewWithTag:1002];

        }
        cell.textLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmlsinger];
        cell.detailTextLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
        // custom menu view
        NSString *nibName = @"ABCellMailStyleMenuView";
        ABCellMenuView *menuView = [ABCellMenuView initWithNib:nibName bundle:nil];
        menuView.delegate = self;
        menuView.indexPath = indexPath;
        cell.rightMenuView = menuView;
        return cell;
}


   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
   }

这些是单元格类中的方法 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer和 - (void)swipeGesture:(UIPanGestureRecognizer *)手势和UIPanGestureRecognizer * _swipeGesture;

2 个答案:

答案 0 :(得分:1)

您需要在UITableView上应用手势,并使用indexPathForRowAtPoint查找单元格。

点击此链接 - http://jademind.com/blog/posts/swipe-gestures-on-uitableview/

答案 1 :(得分:0)

您需要添加UISwipeGesture。你可以通过两种方式实现。将UITableViewCell添加到UITableViewUISwipeGesture。在这里,我只解释在UITableView上添加UISwipeGesture,因为在这种情况下,您需要添加UISwipeGesture一次。

如何在UITableView中添加//Add a left swipe gesture recognizer UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [self.tableView addGestureRecognizer:recognizer]; [recognizer release]; //Add a right swipe gesture recognizer recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; recognizer.delegate = self; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [self.tableView addGestureRecognizer:recognizer]; [recognizer release];

UISwipeGestureRecognizer

这是indexPath的选择器方法。如果你有index Image Array -(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView]; NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; [imageArray removedObjectAtIndexPath:swipedIndexPath.row]; [tableView reloadData]; } ,那么你将imageArray

array of images

我认为for(r=1; r<=num; r+=2) // increment by 2, work for r= 1,3,5,7... { for(c=1; c<=r; c+=2)// increment by 2 printf("%d",r); printf("\n"); } 2

希望这对你有所帮助。