0

我正在开发一个应用程序,用户可以在其中输入产品和这些产品的信息。产品的所有信息都输入到自定义 UITableViewCell 中。我还希望允许用户添加产品图像。为此,我需要显示一个包含 UIImagePickerController 的弹出视图。当我这样做时,Xcode 给了我这个错误:

无法从没有窗口的视图中呈现弹出框。

当用户点击按钮添加(称为addImage)图像时,我的自定义单元格会在我的 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];

}

我的 cellForRowAtIndexPath 看起来像:

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

(“模型”是一个自定义类。该类的每个实例代表一个产品。每次用户向 tableview 添加一行时,该类的一个实例被添加到数组中。)

我整天都在搜索 SO 和 google,但我还没有找到任何关于它如何与自定义单元格一起工作的解决方案,只有关于它在 didSelectRowAtIndexPath 中的工作方式以及触摸披露按钮时的工作方式。

所以我的投入很快:当点击自定义单元格内的按钮时,如何正确显示弹出视图?

提前感谢您,任何帮助将不胜感激。

4

1 回答 1

0

您正在该方法中创建 CustomCell 的新实例。此实例未显示在屏幕上的任何位置。您需要找到代表要从中显示弹出框的行的 CustomCell 实例。如果此行是静态的并且始终相同,则可以执行以下操作:

// 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];

如果您事先不知道索引路径,请将其作为参数传递给addImage:.

编辑

从您更新的问题来看,您似乎混淆了 MVC 的不同部分。可以将按钮添加到自定义单元格,但出于可重用性目的,您不应从那里处理点击。这是您的视图控制器应该做的事情。视图控制器如何知道按钮何时被点击?代表团

于 2013-06-25T15:00:30.180 回答