当前位置: 首页 > 知识库问答 >
问题:

从xib在自定义单元格上设置IBAction?

松新
2023-03-14

我在xib中创建了一个自定义单元格(在iOS6中使用Storyboards,但为单元格创建了单独的xib),现在我正在尝试将扬声器按钮连接到UITableViewController子案例中的IBAction。

我在viewDidLoad中注册了手机:

[self.tableView registerNib:[UINib nibWithNibName:@"MissedWordTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MissedWordCell"];

我尝试了几种不同的方法来添加目标。例如,在我的表视图:单元格中,我尝试直接添加目标。

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

共有1个答案

孟选
2023-03-14

在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
}
 类似资料:
  • 我按照本教程创建了一个自定义。xib,我计划在表格视图的单元格中使用它: https://medium.com/@brianclouser/swift-3-creating-a-custom-view-from-a-xib-ecdfe5b3a960 以下是我创建的 .xib 类: 以前,我在情节提要中创建我的表视图单元格,但我已经意识到我想要一个更灵活的视图,以便我可以在应用的不同部分使用它,所以

  • 问题内容: 我正在使用python 2.7和xlwt模块进行excel导出 我想设置一个我可以使用的单元格的背景色 但我想设置自定义颜色smth。例如#8a8eef或是否存在可能的颜色调色板,因为浅蓝色无法正常工作:) 谢谢 问题答案: 如果您不使用而是逐步构建对象,则这是使用用户友好颜色名称的另一种方法:

  • 我正在使用JavaApachePOI生成excel,我只需要美化它(带边框) 这是我生成excel的一些代码

  • 我有一个带有表视图的Xib(我使用的是Xib而不是常规的故事板,因为我将Xib插入到UIPageView控制器中)。在 Xib 的类中,我注册了一个自定义表视图单元格。尽管单元格插入到表视图中,但它不会根据其约束正确调整大小,也不会更改表视图的显示。 这是我在视图控制器中的设置代码,它具有表视图xib:类 当我运行应用程序时,它看起来像这样: 所以基本上,自定义单元格是从xib文件添加到表格视图中

  • 进入具体播放器编辑页面,点击右侧菜单标签,设置播放器右侧控制菜单。 系统内置了三个菜单:设置、分享按钮、下载(需配合加密播放器使用)。若不需要显示,点击菜单后面的停用即可; 此外,您还可以根据自己的需求来自定义设置右侧菜单,向播放用户开放更多设置效果。 自定义右侧菜单支持以下三种按钮类型,你可以根据自己需求选择; ·超级链接:设置一个链接地址,点击按钮时将打开该链接地址; ·弹出内容:点击按钮时,

  • 请求header 新增自定义菜单 POST /v1/activities/{频道id}/saveMenu Authorization:Bearer {ACCESS TOKEN} Content-Type:application/json 修改自定义菜单 PUT /v1/activities/{频道id}/saveMenu/{菜单id} Authorization:Bearer {A