我在xib中创建了一个自定义单元格(在iOS6中使用了Storyboard,但为单元格创建了单独的xib),现在我正在尝试将我的扬声器按钮连接到我的UITableViewController sublcass中的IBAction。
我在viewDidLoad中注册了我的单元格:
[self.tableView registerNib:[UINib nibWithNibName:@"MissedWordTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MissedWordCell"];
我尝试了几种不同的方法来添加目标。例如,在我的tableView:cellForRowAtIndexPath中,我尝试直接添加目标。
static NSString *CellIdentifier = @"MissedWordCell";
MissedQuestionEntity *missedQuestion;
// forIndexPath: is iOS6
MissedWordTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Add target here? Didn't work. @selector(playWordAction) or @selector(playWordAction:)
// [cell.playAudioBtn addTarget:self action:@selector(playWordAction) forControlEvents:UIControlEventTouchUpInside];
我还尝试在我的自定义单元格xib中将文件所有者设置为我的表视图控制器,但仍然无效。
这是我的错误消息:
2013-07-30 07:47:15.833 Spanish[69420:c07] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSArrayI doIt]: unrecognized selector sent to instance
0xec34430'
*** First throw call stack:
(0x231e012 0x172de7e 0x23a94bd 0x230dbbc 0x230d94e 0x1741705 0x6752c0 0x675258 0x736021 0x73657f 0x7356e8
0x6a4cef 0x6a4f02 0x682d4a 0x674698 0x1c0bdf9 0x1c0bad0 0x2293bf5 0x2293962 0x22c4bb6 0x22c3f44 0x22c3e1b
0x1c0a7e3 0x1c0a6
答案 0 :(得分:5)
在 UITableViewCell 中注册属性:
@property (strong, nonatomic) IBOutlet UIButton *playSoundButton;
在 UITableViewController :
中- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"YourCustomCell" bundle:nil] forCellReuseIdentifier:@"YourCustomCell"];
// ...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YourCustomCell"];
[[cell playSoundButton] addTarget:self action:@selector(playWordAction:) forControlEvents:UIControlEventTouchUpInside];
//...
}
-(IBAction) playWordAction:(id) sender
{
// do what you want to
}