Search code examples
iosobjective-cuitableviewuiscrollviewuipopovercontroller

Get image when press a cell in tableView popover


I try to comprehend iOS dev. and have a question...here... I have a simple scrollview gallery with pageControl(array of images) and have a custom button which show popover with tableView and names of images. I want following...When press some cell with name of image, return to scrollview gallery on image which i need... Thanks for help and answers..

Code of ScrollView and PageControl:

-(void)scrollViewMethod {

    for(UIView* view in [scrollView subviews])
    {
        [view removeFromSuperview];
    }

    for ( int i = 0; i < [imgArray count]; i++) {

        //Create objects in pages
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        self.imgView = [[UIImageView alloc]initWithFrame:frame];
        self.imgView.image = [UIImage imageNamed:[imgArray objectAtIndex:i]];
        self.imgView.contentMode = UIViewContentModeCenter;
        [self.scrollView addSubview:self.imgView];

    }

}


-(void)scrollViewDidScroll:(UIScrollView *)sender {

    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
    //self.pageControl.numberOfPages
    // Disable vertical scroll
    [sender setContentOffset:CGPointMake(sender.contentOffset.x, scrollY)];
}

And Code of custom button and popover:

-(IBAction)showTable:(id)sender {

    UIImage *btnTableImage = [UIImage imageNamed:@"list.png"];

    UIButton *bttn = [UIButton buttonWithType:UIButtonTypeCustom];

    bttn.bounds = CGRectMake( 0, 0, btnTableImage.size.width, btnTableImage.size.height );

    [bttn addTarget:self action:@selector(showTable:) forControlEvents:UIControlEventTouchDown];

    [bttn setImage:btnTableImage forState:UIControlStateNormal];

    UIBarButtonItem *buttonListItem = [[UIBarButtonItem alloc] initWithCustomView:bttn];

    [self.navigationItem setLeftBarButtonItem:buttonListItem];


    if (self._popover) {
        [self._popover dismissPopoverAnimated:YES];
        self._popover = nil;
        return;
    }
    SelectImageTableController *sitc = [self.storyboard instantiateViewControllerWithIdentifier:@"SelectImageTableController"];
    sitc.delegate = self;
    self._popover = [[UIPopoverController alloc]initWithContentViewController:sitc];
    self._popover.delegate = self;
    [self._popover presentPopoverFromBarButtonItem:buttonListItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

And tableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [imgList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [imgList objectAtIndex:indexPath.row];
    //cell.imageView.image = [UIImage imageNamed:[ivc.imgArray objectAtIndex:indexPath.row]];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark Table View Delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //ImageViewController *ivc = [[ImageViewController alloc]init];
    NSLog(@"row: %d %@", indexPath.row, cell.textLabel.text);
}

Solution

  • You have indexPath of selected cell. X coordinate of each image equals to self.scrollView.frame.size.width * i. So You can use [self.scrollView scrollToRect:rect animated:NO] where Your

    i = indexPath.row;
    rect.origin.x = self.scrollView.frame.size.width * i;
    frame.size = self.scrollView.frame.size;
    

    Put it in didSelect method of tableviewdelegate.