Yuv*_*j.M 8 parameters objective-c uibutton uitableview ios
我加入UIButton了UITableViewCells.我有用户单击按钮时,我们已获得索引路径以使用来自的值NSMutableArray.我用下面的方法来获取当前的信息IndexPath,
[getMeButton addTarget:self action:@selector(resendTheErrorMessage:) forControlEvents:UIControlEventTouchUpInside];
-(void) resendTheErrorMessage:(id)sender
{
NSLog(@"SEnder: %@", sender);
//NSLog(@"Index Path : %@", indexpath);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我传递当前的indexpath UIButton's@selector.提前致谢.
编辑:
这是我得到的输出 NSLog()
<UIButton: 0x864d420; frame = (225 31; 95 16); opaque = NO; tag = 105; layer = <CALayer: 0x864d570>>
Run Code Online (Sandbox Code Playgroud)
The*_*ger 30
添加你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];
Run Code Online (Sandbox Code Playgroud)
然后得到它的标签号码 -
-(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];
}
Run Code Online (Sandbox Code Playgroud)
注意: 当tableView只有1个部分时,上述解决方案将起作用.如果你的tableView有多个部分,你应该知道你的部分索引或者去找下面的方法.
替代方案:1
UIView *contentView = (UIView *)[sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tblView indexPathForCell:cell];
Run Code Online (Sandbox Code Playgroud)
备选:2
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
假设按钮是单元视图的子视图,并且表视图知道单元视图的索引路径,这样的东西可以工作:
- (UITableViewCell *)containingCellForView:(UIView *)view
{
if (!view.superview)
return nil;
if ([view.superview isKindOfClass:[UITableViewCell class]])
return (UITableViewCell *)view.superview;
return [self containingCellForView:view.superview];
}
- (IBAction)buttonClicked:(id)sender
{
UITableViewCell *containingCell = [self containingCellForView:sender];
if (containingCell) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:containingCell];
NSLog(@"Section: %i Row: %i", indexPath.section, indexPath.row);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15728 次 |
| 最近记录: |