0

我在自定义单元格中有一个用于 TableView 的按钮,它应该打开相机拍照。

我想到了两种方法,但无法使它们起作用。第一个是从单元格内打开 UIImagePickerController 的一个实例。嗯,好像不能打电话

[self presentViewController...];

从细胞内。这是正确的吗?

由于这个“结果”,我想把打开 UIImagePickerController 的方法放在 TableViewController 中,然后从单元格(按钮所在的位置)中调用这个方法,比如

[super openCamera];

或者使 TableViewController 成为单元格的委托,以使其能够调用该方法。

这些想法是否朝着正确的方向发展?你会推荐什么?非常感谢你!

4

2 回答 2

0

好的,我想出了一些办法,但我仍然想知道是否可以更轻松地完成。这是我找到的解决方案:

在我添加的自定义单元格中

@property (nonatomic, assign) id adminController;

然后在 tableViewController 中,我自定义了以下方法以使用我创建的自定义单元格并将 tableViewController 设置为“admin”

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    CreateCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    cell.adminController = self;

    return cell;
}

所以我终于可以打电话了

[self.adminController performSelector:@selector(openCamera)];
于 2013-05-31T13:25:20.627 回答
0

这是一个老问题,但我也想回答我的老问题......是的,有一种使用块的更简单方法:

首先,在 UITableViewCell 接口中声明一个公共方法:

@interface YourCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIButton *button;

- (void)setDidTapButtonBlock:(void (^)(id sender))didTapButtonBlock;

@end

在 UITableViewCell 子类实现文件中声明一个带有复制属性的私有属性。

#import "YourCell.h"

@interface YourCell ()

@property (copy, nonatomic) void (^buttonTappedBlock)(id sender);

@end

在 UITableViewCell 构造函数中添加 UIControl 的 target 和 action 并实现 selector 方法

- (void)awakeFromNib {
    [super awakeFromNib];

    [self.button addTarget:self 
                    action:@selector(didTapButton:)  
          forControlEvents:UIControlEventTouchUpInside];
}

- (void)didTapButton:(id)sender {
    if (buttonTappedBlock) {
        buttonTappedBlock(sender);
    }
}

最后在控制器中实现tableView:cellForRowAtIndexPath:方法中的block代码

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    YourCell *cell = (YourCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                 forIndexPath:indexPath];

    [cell buttonTappedBlock:^(id sender) {
        NSLog(@"%@", item[@"title"]);
    }];

    return cell;
}

有关块的更多信息,您可以阅读使用块

于 2014-12-05T05:53:27.143 回答