iPhone - 如何确定在自定义 UITableViewCell 中按下按钮的单元格

2023-07-07移动开发问题
3

本文介绍了iPhone - 如何确定在自定义 UITableViewCell 中按下按钮的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前有一个 UITableView,其中填充了一个单独的 nib 中的自定义 UITableViewCell.在单元格中,有两个按钮连接到自定义单元格类中的操作.当我单击其中一个按钮时,我可以调用正确的方法,但我需要知道按钮被按下的是哪一行.每个按钮的标记属性为 0.我不需要知道整个单元格的时间被选中,只是在按下特定按钮时,所以我需要知道它是哪一行,这样我才能更新正确的对象.

I currently have a UITableView that is populated with a custom UITableViewCell that is in a separate nib. In the cell, there are two buttons that are wired to actions in the custom cell class. When I click one of the buttons, I can call the proper method, but I need to know which row the button was pressed in. The tag property for each button is coming as 0. I don't need to know when the entire cell is selected, just when a particular button is pressed, so I need to know which row it is so I can update the proper object.

推荐答案

更简单的解决方案是使用 (id)sender 定义按钮回调 并使用它来挖掘表格行索引.下面是一些示例代码:

Much easier solution is to define your button callback with (id)sender and use that to dig out the table row index. Here's some sample code:

- (IBAction)buttonWasPressed:(id)sender
{
    NSIndexPath *indexPath =
        [self.myTableView
         indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
    NSUInteger row = indexPath.row;

    // Do something with row index
}

您很可能使用相同的行索引来创建/填充单元格,因此确定您的按钮现在应该做什么应该是微不足道的.无需玩标签并尽量保持它们井井有条!

Most likely you used that same row index to create/fill the cell, so it should be trivial to identify what your button should now do. No need to play with tags and try to keep them in order!

澄清:如果您当前使用-(IBAction)buttonWasPressed;只需将其重新定义为-(IBAction)buttonWasPressed:(id)sender; 额外的回调参数就在那里,不需要做任何额外的事情来获取它.还记得将您的按钮重新连接到 Interface Builder 中的新回调!

Clarification: if you currently use -(IBAction)buttonWasPressed; just redefine it as -(IBAction)buttonWasPressed:(id)sender; The additional callback argument is there, no need to do anything extra to get it. Also remember to reconnect your button to new callback in Interface Builder!

这篇关于iPhone - 如何确定在自定义 UITableViewCell 中按下按钮的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7