Megz
Megz

Reputation: 47

Get click of imageview(added dynamically) within a row of uitableview in ios

I have a tableview whose rows are dynamic and each row have n numbers of imageviews as it is in the screenshot attached Row of the tableview cell

Now what I want is to know which imageview I have clicked.

NOTE : imageview is added dynamically to one view and that view is added to scrollview so that it can scroll horizontally.

And there are n numbers of row in a tableview.

EDIT: I tried adding simple button as well above the image view just to try out if that clicks but its click also didn't work.

I tried the solution of Gesture as well but that also didn't work

CODE:

-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strrowId ;

    UITableViewCell  *cell = [_tblviewScroll1 dequeueReusableCellWithIdentifier:@"cellOne"];


        // create and add labels to the contentView
        [cell.contentView setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

       // cell.contentView.userInteractionEnabled=NO;
        scrollView1 = (UIScrollView*)[cell.contentView viewWithTag:3];

        [scrollView1  setShowsVerticalScrollIndicator:NO];

        scrollView1.delegate = self;

        NSArray* subviews1 = [scrollView1 subviews];
            for (UIView* subview in subviews1) {
                [subview removeFromSuperview];
            }

        if (scrollView1.contentOffset.y > 0  ||  scrollView1.contentOffset.y < 0 )
            scrollView1.contentOffset = CGPointMake(scrollView1.contentOffset.x, 0);



    [scrollView1 setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]];

    [scrollView1 setCanCancelContentTouches:NO];

    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleBlack;

    scrollView1.clipsToBounds = YES

    scrollView1.scrollEnabled = YES;




        for (int i = 0; i <mutSubTitle.count; i++)
        {
            NSString *ids = @"";



                UIView *viewvertically1;
                viewvertically1=[[UIView alloc]init];
                viewvertically1.tag = (i+1);
                [viewvertically1 setUserInteractionEnabled:YES];
                [viewvertically1 setBackgroundColor:[UIColor whiteColor]];

                //Displaying image

                NSString *strImgUrl = @"https://img.xxxx.com/";
                NSString *strimge=[mutSubImag objectAtIndex:i];
                strImgUrl = [strImgUrl stringByAppendingString:strimge];

                NSURL *url = strImgUrl;

                UIImage *image = [UIImage imageNamed:url];

                UIImageView *imageView = [[UIImageView alloc] initWithImage:image];


                 imageView.frame= CGRectMake(0, 10, 170, 110);

                imageView.tag = i;  // tag our images for later use when we place them in serial fashion
                [imageView setUserInteractionEnabled:YES];

                // images with Lazy Loading

                [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];


                UITapGestureRecognizer *imgTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTapEvent:)];


                imgTapGesture.numberOfTouchesRequired = 1;


                [imageView addGestureRecognizer:imgTapGesture];

                [viewvertically1 addSubview:imageView];
                [viewvertically1 addSubview:lblDesc];

                [scrollView1 addSubview:viewvertically1];
                //[cell bringSubviewToFront:scrollView1];

        }

    return cell;
}

Upvotes: 1

Views: 669

Answers (3)

Vijay Palwe
Vijay Palwe

Reputation: 109

Use UIButton as @Arun says or the imageview ur using currently add button on Image with clear background color and add tag to that button and use that click action.

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

In your cellForRowAtIndexPath method add the UITapGestureRecognizer for your Concept

// by default the imageview userInteraction is disable you need to manually enable
cell.yourimageView.userInteractionEnabled = YES;
// the following line used for assign the different tags for eachImage
cell.yourimageView.tag = indexPath.row;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageTapped:)];
tap.numberOfTapsRequired = 1;
[cell.yourimageView addGestureRecognizer:tap];  

finally need to check which imageView was clicked, check the flag in selector method

-(void)ImageTapped :(UITapGestureRecognizer *) gesture
{
  // you can get tag in which image is selected
     NSLog(@"Tag = %d", gesture.view.tag);
}

Upvotes: 1

Arun
Arun

Reputation: 1411

Try giving a button instead of image view added to a view. Assign the image to button's backgroundImageView. You can then give actions for the buttons

Upvotes: 0

Related Questions