【问题标题】:calling didSelectRowAtIndexPath when i uitableviewcell is swipe当我 uitableviewcell 滑动时调用 didSelectRowAtIndexPath
【发布时间】:2015-04-23 11:28:34
【问题描述】:

我在我的应用程序中使用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 *)gesture 和 UIPanGestureRecognizer *_swipeGesture;

【问题讨论】:

    标签: ios objective-c iphone uitableview didselectrowatindexpath


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

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

    如何在UITableView 中添加UISwipeGesture

    //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你的ImageArray

    -(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

    希望对您有所帮助。

    【讨论】:

    • 如果您卡在任何地方,请告诉我。
    • 试过这个但还是不行我什至在didSwipe中放了一个nslog但它甚至没有调用。
    • 您是否在您的UItableView 中添加了UISwipeGestureRecognizer
    • 在我的故事板中紧吗?是的,我做到了
    • 我不知道装订或什么天气问题。我想建议您可以像上面的代码一样以编程方式在 viewDidLoad 方法中添加 UISwipeGesture。并检查它的选择器方法是否被调用?
    猜你喜欢
    • 2014-05-02
    • 1970-01-01
    • 2023-04-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多