Ios 选择器中方法参数的使用
我正在Ios 选择器中方法参数的使用,ios,Ios,我正在表视图中创建自定义按钮,在该按钮的操作中,我希望打印特定行的索引路径。我通过选择器调用该方法 代码: -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier=@""; UITableViewCell *cell=[tableView dequeueReusableC
表视图中创建自定义按钮
,在该按钮的操作中,我希望打印特定行的索引路径
。我通过选择器调用该方法
代码:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier=@"";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// cell.textLabel.text=@"Hello";
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2, cell.contentView.frame.size.height/2-10, 20, 30)];
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-20, 0, 20, cell.contentView.frame.size.height-20)];
[btn setBackgroundImage:[UIImage imageNamed:@"band"] forState:UIControlStateNormal];
[ btn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside ];
[cell.contentView addSubview:btn];
[cell.contentView addSubview:img];
img.image=[UIImage imageNamed:@"band"];
}
return cell;
}
-(void)change:(NSIndexPath *)idd
{
NSLog(@"%@",idd);
}
问题:如何在选择器中传递参数
nsindepath
我相信按钮只能携带一个输入,所以将indexpath设置为btn标记并使用它
btn.tag=indexPath.row;
[ btn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside ];
- (void) change:(id)sender
{
NSLog(@"%d", sender.tag);
}
如果我调用这样的方法:
[var-method]代码>在这里我可以传递参数。我们不能使用选择器进行同样的操作。@gamerlegend您不能将参数传递给change:here。如果您确实想使用indexpath,那么请使用Joe提到的customtableview单元格或使用category类。看看这个答案