xcode 从自定义 UITableViewCell 的按钮获取按钮按下
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9643642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a button press from a custom UITableViewCell's button
提问by James V
I have a project with a custom UITableViewCell (cell.h/.m/.xib) that has 2 labels (labelMain/labelSub), and 2 buttons on it (buttonName/buttonInfo) linked to 2 actions (showName/showInfo).
我有一个带有自定义 UITableViewCell (cell.h/.m/.xib) 的项目,它有 2 个标签 (labelMain/labelSub),上面有 2 个按钮 (buttonName/buttonInfo) 链接到 2 个动作 (showName/showInfo)。
What I want to do is be able to access the 2 actions in my projects main viewcontroller so that when showName is pressed, a textfield.text in the viewcontroller (not in the cell) is set to that specific cell's labelMain.text.
我想要做的是能够访问我的项目主视图控制器中的 2 个操作,以便在按下 showName 时,视图控制器(不在单元格中)中的 textfield.text 被设置为该特定单元格的 labelMain.text。
Hope that makes sense. My problem is that if I write the action (showName) in the cell.m, I cannot access the textfield from my main viewcontroller. On the flip side, if I write the action in my viewcontroller, how do I know which button inside which cell was tapped?
希望这是有道理的。我的问题是,如果我在 cell.m 中写入操作 (showName),则无法从主视图控制器访问文本字段。另一方面,如果我在视图控制器中编写操作,我如何知道点击了哪个单元格内的哪个按钮?
Hope this makes sense...
希望这是有道理的...
回答by HelmiB
Use tag
can identify which button in which cell is being tapped.
使用tag
可以识别正在点击哪个单元格中的哪个按钮。
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//init identifier
if (cell ==nil)
{
//load nib file
}
buttonName.tag = indexPath.row;
[buttonName addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchUpInside];
buttonInfo.tag = indexPath.row;
[buttonInfo addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void) showName:(UIButton*)button{
int row = button.tag; //you know that which row button is tapped
}
-(void)showInfo:(UIButton*)button{
int row = button.tag;//you know that which row button is tapped
}
---------------- EDIT-------------
---------------- 编辑-------------
If you need to know which button is pressed based on row & section, you may try this below. (in cellForRowAtIndexPath:
method)
如果您需要根据row & section知道按下了哪个按钮,您可以在下面尝试。(在cellForRowAtIndexPath:
方法中)
int tag = (indexPath.row+1)+(indexPath.section*100);
buttonName.tag = tag;
When button at
当按钮在
row = 5, section = 0 then tag = 6.
row = 4, section = 3 then tag = 305.
row = 0, section = 11 then tag =1101.
行 = 5,部分 = 0 然后标签 = 6。
行 = 4,部分 = 3 然后标签 = 305。
行 = 0,部分 = 11 然后标记 =1101。
limitation, row cannot be more than 99. and DON'T use positive tag for other view. if you need use tag, try negative. (-1, -9, -100).
限制,行不能超过 99。其他视图不要使用正标签。如果您需要使用标签,请尝试否定。(-1、-9、-100)。
so from here, you can calculate back row & section of indexPath. using this :
所以从这里,您可以计算 indexPath 的后排和部分。使用这个:
-(NSIndexPath*)getIndexPathFromTag:(NSInteger)tag{
/* To get indexPath from textfeidl tag,
TextField tag set = (indexPath.row +1) + (indexPath.section*100) */
int row =0;
int section =0;
for (int i =100; i<tag; i=i+100) {
section++;
}
row = tag - (section*100);
row-=1;
return [NSIndexPath indexPathForRow:row inSection:section];
}
how to use :
如何使用 :
-(void)showInfo:(UIButton*)button{
NSIndexPath *indexPath = [self getIndexPathFromTag:button.tag];
int row = indexPath.row;
int section = indexPath.section;
}
回答by hchouhan02
Try this in viewController.m file in cellForRowAtIndexPath
method
在cellForRowAtIndexPath
方法中的 viewController.m 文件中试试这个
[[customCell showNameBtn] setTag:indexPath.row];
[[customCell showNameBtn] addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchDown];
-(void) showName:(id)sender
{
// check here the sender tag and then perform you action what you want.
}
回答by Ajith Kumar
I suggest to use delegate(protocol) method for this. You will be having full control on the button, cell and the view controller. And its very simple too
我建议为此使用委托(协议)方法。您将完全控制按钮、单元格和视图控制器。而且它也很简单
回答by Tib
The simplest solution in my opinion:
我认为最简单的解决方案:
In your CustomCell.h
:
在您的CustomCell.h
:
@property (nonatomic, copy) void (^buttonTapAction)(id sender);
In your CustomCell.m
create the IBAction
and connect it to your UIButton
:
在您CustomCell.m
创建IBAction
并将其连接到您的UIButton
:
-(IBAction)cellButtonIsPressed:(id)sender{
if (self.buttonTapAction) {
self.buttonTapAction(sender);
}
}
Then in the ViewController.m
where your UITableView
is managed :
然后在ViewController.m
您UITableView
管理的地方:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = @"customCell";
CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.buttonTapAction = ^(id sender){
//Do what you want to do when the button inside THIS cell is clicked
};
}
回答by Sanket Pandya
Try to create the button dynamically, with its methods in same controller. Do this in cellForRowAtIndexPath:
method:
尝试动态创建按钮,其方法在同一控制器中。在cellForRowAtIndexPath:
方法中执行此操作:
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WorkRequestedCC" owner:self options:nil];
{
for (id oneObject in nib) if ([oneObject isKindOfClass:[WorkRequestedCC class]])
cell = (WorkRequestedCC *)oneObject;
}
UILabel *costLbl=[[UILabel alloc]initWithFrame:CGRectMake(792, 13, 10, 15)];
costLbl.text=@"$";
[costLbl setBackgroundColor:[UIColor clearColor]];
UITextField *txtCost=[[UITextField alloc]initWithFrame:CGRectMake(805, 10, 94, 27)];
txtCost.backgroundColor=[UIColor whiteColor];
[txtCost setKeyboardType:UIKeyboardTypeNumberPad];
txtCost.text=obj.expCost;
[txtCost addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
[cell.contentView addSubview:costLbl];
[cell.contentView addSubview:txtCost];
cell.lblDes.text=obj.expDes;
}
- (void)textChanged
{
//your code here
}