2013-05-31 30 views
0

我有一个自定义单元格中的按钮,应该打开相机拍照。从UITableView单元内打开相机

我想到了两种方式,但无法让它们工作。 第一个是从单元格内打开UIImagePickerController的实例。好吧,好像我不能拨打

[self presentViewController...]; 

从单元格内。这是正确的吗?

因为这个“结果”我想过把它打开了的UIImagePickerController的TableViewController内,然后从细胞(如按钮的位置)的东西中调用此方法像

[super openCamera]; 

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

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

回答

0

好吧,我想出了一些东西,但我仍然想知道它是否可以做得更容易。 这里是我找到了解决办法:

在定制单元我加入

@property (nonatomic, assign) id adminController; 

然后在tableViewController我定制下面的方法来使用我创建的自定义单元格,并设置tableViewController ALS“管理员”

- (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)]; 
+0

在哪里写这行[self.adminController performSelector:@selector(openCamera)]; ?? – Muju

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 

添加在靶和UIControl的动作在的UITableViewCell构造并实现选择方法

- (void)awakeFromNib { 
    [super awakeFromNib]; 

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

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

最后实现在的tableView所述块码:的cellForRowAtIndexPath:在控制器方法

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

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

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

    return cell; 
} 

有关块的更多信息,请参阅Working With Blocks