我在 xib 中创建了一个自定义单元格(在 iOS 6 中使用 Storyboard,但为单元格创建了单独的 xib),现在我尝试将扬声器按钮连接到 UITableViewController 子类中的 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
In UITableViewCell只需注册财产:
@property (strong, nonatomic) IBOutlet UIButton *playSoundButton;
In your 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)