我在TableView的自定义Cell中有一个按钮,它应该打开相机拍照。
我想到了两种方法但却无法让它们发挥作用。 首先是从单元格中打开UIImagePickerController的实例。好吧,好像我不能打电话
[self presentViewController...];
来自细胞内部。这是对的吗?
由于这个“结果”我想到了在TableViewController中放置打开UIImagePickerController的方法,然后从单元格(按钮所在的位置)调用此方法,如
[super openCamera];
或者让TableViewController成为单元格的委托,以使其能够调用该方法。
这些想法是否朝着正确的方向发展?你会推荐什么?非常感谢你!
答案 0 :(得分:0)
好的,我想出了什么,但我仍然想知道它是否可以更轻松地完成。 这是我找到的解决方案:
在我添加的自定义单元格中
@property (nonatomic, assign) id adminController;
然后在tableViewController中我定制了以下方法来使用我创建的自定义单元格并设置tableViewController als“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)];
答案 1 :(得分:0)
这是一个古老的问题,但我想回答我的旧问题......是的,使用块的方法更简单:
首先,在UITableViewCell接口中声明一个公共方法:
@interface YourCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *button;
- (void)setDidTapButtonBlock:(void (^)(id sender))didTapButtonBlock;
@end
在UITableViewCell子类实现文件中,声明具有copy属性的私有属性。
#import "YourCell.h"
@interface YourCell ()
@property (copy, nonatomic) void (^buttonTappedBlock)(id sender);
@end
在UITableViewCell构造函数中添加UIControl的目标和操作,并实现选择器方法
- (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