0

I'm developing an app in which the user can enter products and information on those products. All information on a products gets entered in a custom UITableViewCell. I also want to allow the user to add an image of products. To do that, I need to show a popover view containing UIImagePickerController. when I do that, Xcode giver me this error:

Popovers cannot be presented from a view which does not have a window.

When the user taps the button to add an(called addImage) image, my custom cell triggers this action inside my TableView:

- (void) addImage
{
    CustomCell *customcell = [[CustomCell alloc] init];

    itemImagePicker = [[UIImagePickerController alloc] init];
    itemImagePicker.delegate = self;
    itemImagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;

    itemImagePopover = [[UIPopoverController alloc] initWithContentViewController:itemImagePicker];
    [itemImagePopover presentPopoverFromRect:customCell.addImage.bounds inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

my cellForRowAtIndexPath looks like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
                                                     owner:self options:nil];
        for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
            cell = (CustomCell *)oneObject;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSUInteger *row = [indexPath row];
    Model *model = self.products[indexPath.row];

    cell.itemName.text = model.itemName;
    cell.itemDescription.text = model.itemDescription;
    cell.itemPrice.text = model.itemPrice;

    cell.itemPrice.delegate = self;
    cell.itemName.delegate = self;
    cell.itemDescription.delegate = self;

    NSLog(@"%@", cell.itemPrice);

    return cell;
}

("model" is a custom class. each instance of that class represents one product. everyctime the user adds a row to the tableview, one instance of this class gets added to an array.)

I have been searching SO and google all day, and I haven't found any solution on how this works with a custom cell, only on how it works in the didSelectRowAtIndexPath and when a disclosure button is touched.

so my put is shortly: How do I properly show a popover view when A button inside a custom cell is tapped?

Thank you in advance, any help would be much appreciated.

Joris416
  • 4,751
  • 5
  • 32
  • 59

1 Answers1

0

You are creating a new instance of CustomCell in that method. This instance isn't shown anywhere on screen. You need to find the instance of CustomCell that represents the row from which you want to show the popover. If this row is static and will always be the same, you can do something like this:

// change the row and section variables below to the values that correspond to the section and row from which you want to display the popover
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
CustomCell *customCell = [self.tableView cellForRowAtIndexPath:indexPath];

If you don't know the index path beforehand, pass it as a parameter to addImage:.

EDIT

From your updated question, it appears that you're confusing the different parts of MVC. It's fine to add the button to your custom cell, but for reusability purposes you shouldn't handle the tapping from there. That's something your view controller should do. How does the view controller know when the button is tapped? Delegation.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • <#row>? <#section>? what are those two exactly? i've never seen them before (i'm a beginner). I'm afraid it doesn't work, is gives me an "expected expression" error for that line of code/ – Joris416 Jun 25 '13 at 15:14
  • You need to fill in the values for the row and section that corresponds to the cell from which you want to show the popover. I'll edit for clarity. – Scott Berrevoets Jun 25 '13 at 15:15
  • ah that clarifies. can you explain me how to pass the indexpath as a parameter to that method? – Joris416 Jun 25 '13 at 15:20
  • Change the method to `- addImage:(NSIndexPath *)indexPath`, and when you call it pass the right index path. If you're still not sure how to do it then, please update your question with where you call `addImage` from and what your tableview looks like. – Scott Berrevoets Jun 25 '13 at 15:25
  • i have never done this before. I will update my question with the cellForRowAtIndexPath. addImage is called by my CustomClass. – Joris416 Jun 25 '13 at 15:36
  • ok, I will make sure my tableviewcontroller handles the touching. but if I do so, I still need to know at which row it was touched right? what should my method look like when the tableViewController handles touching of this button? – Joris416 Jun 25 '13 at 15:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32346/discussion-between-scott-and-imagine-digital) – Scott Berrevoets Jun 25 '13 at 16:01
  • Let me summarize: You cannot put the popover in cellforrowatindexpath because you cannot open a popover for every row in the table view. You need to put the code in didSelectRowAtIndexPath:(NSIndexPath *)indexPath. In the method call CustomCell *customCell = [self.tableView cellForRowAtIndexPath:indexPath]; then pass it to addImage as follows: -(void) addImage:(CustomCell *) cell …. in your popover change the inView: parameter to inView:cell.view and make sure that the fromRect parameter is in the bounds of the cell. – Paulo Sep 24 '13 at 09:50