在tableView弹出框中按单元格时获取图像

时间:2014-04-23 12:39:25

标签: ios objective-c uitableview uiscrollview uipopovercontroller

我尝试理解iOS开发。并有一个问题......这里......     我有一个简单的scrollview库与pageControl(图像数组),并有一个自定义按钮,显示popover与tableView和图像的名称。我想跟随...当按下图像名称的某个单元格时,返回我需要的图像上的scrollview图库...     感谢您的帮助和解答..

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);
}

2 个答案:

答案 0 :(得分:0)

根据您的问题,我了解到在表格视图单元格中您有图像名称,单击单元格时您必须显示该图像。

现在你还没有提到图片的保存位置,但仍会尝试给出答案。我希望你会对你有所了解。

首先创建图像名称。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cName= [NSString stringWithFormat@"%@.png",cell.textLabel.text];

我在这里考虑将png作为图像的扩展。

1.如果图像保存在Document文件夹中,则

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:cName];
        UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];

2.如果图像保存在Bundle中,即应用程序的资源文件夹中

UIImage *img=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:cell.textLabel.text ofType:@"png"]];

现在img将是您可用于设置imageview图像的图像。

答案 1 :(得分:0)

您拥有所选单元格的indexPath。每个图像的X坐标等于self.scrollView.frame.size.width * i。所以你可以使用[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;

将它放在tableviewdelegate的didSelect方法中。