本文介绍了如何在iOS中通过参数将UITableView IndexPath传递给UIButton @selector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 UITableViewCells
中添加了 UIButton
.我有当用户单击按钮时,我们获得了 indexpath 以使用来自 NSMutableArray
的值.我已经使用下面的来获取当前的 IndexPath
,
I have added UIButton
in UITableViewCells
. I have when the user clicks the button we have get the indexpath to use the values from NSMutableArray
. I have used the below to get the current IndexPath
,
[getMeButton addTarget:self action:@selector(resendTheErrorMessage:) forControlEvents:UIControlEventTouchUpInside];
-(void) resendTheErrorMessage:(id)sender
{
NSLog(@"SEnder: %@", sender);
//NSLog(@"Index Path : %@", indexpath);
}
谁能帮我传递当前的 indexpath UIButton's
@selector.提前致谢.
Can anyone please help me to pass current indexpath UIButton's
@selector. Thanks in advance.
这是我从 NSLog()
<UIButton: 0x864d420; frame = (225 31; 95 16); opaque = NO; tag = 105; layer = <CALayer: 0x864d570>>
推荐答案
添加你的 UIButton
像这样
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10.0, 2.0, 140.0, 40.0)];
[btn setTitle:@"ButtonTitle" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn setTag:indexPath.row];
[cell.contentView addSubview:btn];
然后得到它的标签号——
And then get its tag number -
-(void)buttonClicked:(id)sender
{
NSLog(@"tag number is = %d",[sender tag]);
//In this case the tag number of button will be same as your cellIndex.
// You can make your cell from this.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];
}
注意: 当您的 tableView 只有 1 个部分时,上述解决方案将起作用.如果您的 tableView 有多个部分,您应该知道您的部分索引或使用以下方法.
替代方案:1
UIView *contentView = (UIView *)[sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tblView indexPathForCell:cell];
替代方案:2
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];
这篇关于如何在iOS中通过参数将UITableView IndexPath传递给UIButton @selector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!