1

I have implemented async image view to load images on my custom cell views, but the images are showing as small on cells. So I need to show an enlarged form of that image when clicking on that image views. How can I achieve this?

This is my code:

 if (cell == nil) {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     //create new cell
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

     //common settings
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
     cell.layer.cornerRadius = 10; 
     cell.layer.masksToBounds = YES;

     cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
     cell.imageView.frame = CGRectMake(10.0f, 10.0f, 60.0f, 44.0f);
     cell.imageView.backgroundColor = [UIColor colorWithRed:73 green:73 blue:73 alpha:1];

     cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
     imgBtn=[cell imageView];
     objectAtIndex:indexPath.row]objectForKey:@"Name"];
     cell.imageView.imageURL =  [NSURL URLWithString:[[detailsList objectAtIndex:indexPath.row]objectForKey:@"image_url"]];
}

I want to enlarge the image upon touching the image view inside the cell. Any ideas? Thanks in advance.

shim
  • 9,289
  • 12
  • 69
  • 108
iOS Developer
  • 1,723
  • 2
  • 16
  • 47

4 Answers4

3

You can add image with gesture recognizer as follows:-

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];           
tapped.numberOfTapsRequired = 1;
[image setUserInteractionEnabled:YES];
image.tag = indexPath.row;
[image addGestureRecognizer:tapped];   
[cell.contentView addSubview:image];

And your imagetapped function will be:-

 - (void)imageTapped:(UITapGestureRecognizer *)gesture 
{
    UIImageView *img = (UIImageView *)[gesture view];
    BigimgView.image = img.image
    [self.view addSubview:BigimgView];
    UITableViewCell *cell = [MyTableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:img.tag]];

}

and BigimgView can be iboutlet of imageview with big size.

Dhaval Panchal
  • 2,529
  • 1
  • 25
  • 36
1

You want to receive a message, when you tap on the imageView? A possible solution is to create a custom UITableViewCell, maybe in an own xib file.

Then your problem would be to make the UIImageView on your custom UITableViewCell clickable. Please have a look at this question: here

A simpler solution is to use a UIButton, not a UIImageView. You can set an image/backgroundImage to a UIButton.

Community
  • 1
  • 1
Sebastian
  • 3,379
  • 2
  • 23
  • 39
1

Try using a UIButton:

UIButton * b = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f , cell.imageView.frame.size.width, cell.imageView.frame.size.height)];
b.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[b addTarget:self action:@selector(changeSize:) forControlEvents:UIControlEventTouchUpInside];
[cell.imageView addSubview:b];

-(IBAction)changeSize:(UIButton *)sender
{
    UIImageView * imageView = (UIImageView *)[sender superview];
    imageView.frame = //Your larger frame goes here
}
llama591
  • 453
  • 5
  • 15
1

in your custom cell add the button.

@interface custom : UITableViewCell
{
    UIButton *button1
}

and then add the button in xib also.then give connection to that button.

come to the tableview datasource methods.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    /// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    custom *cell=(custom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        NSArray *topView=[[NSBundle mainBundle]loadNibNamed:@"custom" owner:self options:nil];

        for(id currentobject in topView)
        {
            if ([currentobject isKindOfClass:[UITableViewCell class]])
            {
                cell=(custom *)currentobject;
                //cell.backgroundView.backgroundColor=[UIColor clearColor];

                 break;
             }
         }
    }   
    //  0.0f, 0.0f , cell.imageView.frame.size.width,        cell.imageView.frame.size.height
    [cell.button1 addTarget:self action:@selector(changeSize) forControlEvents:UIControlEventTouchUpInside];
}

now click the button.it will go to that method.,., try it.,.,

-(IBAction)changeSize:(UIButton *)sender
{
    UIImageView * imageView = (UIImageView *)[sender superview];
    imageView.frame = //Your larger frame goes here
}

u need to give the background image for the cell.button1 it may help u.,.

i am learning.,

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Manikandan
  • 321
  • 2
  • 12