Objective c uitable视图按钮操作

Objective c uitable视图按钮操作,objective-c,ios,uitableview,Objective C,Ios,Uitableview,在我的表格视图中,每个单元格中都有一个按钮 UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom]; editButton.frame = CGRectMake(281,0, 20, 50); UIImage *editButtonImage = [UIImage imageNamed:@"edit.png"]; [editButton setImage:editButtonImage forState:UICont

在我的表格视图中,每个单元格中都有一个按钮

UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(281,0, 20, 50);
UIImage *editButtonImage = [UIImage imageNamed:@"edit.png"];
[editButton setImage:editButtonImage forState:UIControlStateNormal];
[editButton setBackgroundColor:[UIColor clearColor]];
[editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:editButton];
这个按钮是什么

-(IBAction)editButtonAction:(id)sender{
     UIButton *btn = (UIButton*) sender;
     UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;
     NSIndexPath *indexPath =[self.table indexPathForCell:cell]; 
     int row = indexPath.row;
     NSLog(@"Selected row is: %d",row);
}
当我点击表格视图中的按钮时,选中的行总是给出0 你知道吗?以及任何代码

替代解决方案:

.
.
editButton.tag = indexPath.row;
[editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
.
.

-(IBAction)editButtonAction:(id)sender{
UIButton *btn = (UIButton*) sender;
NSLog(@"Selected row is: %d",btn.tag);
}
替代解决方案:

.
.
editButton.tag = indexPath.row;
[editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
.
.

-(IBAction)editButtonAction:(id)sender{
UIButton *btn = (UIButton*) sender;
NSLog(@"Selected row is: %d",btn.tag);
}

您可以创建按钮分配

将按钮标记设置为tableView单元格的索引

[editButton setTag:indexPath.row];
在按钮

-(IBAction)editButtonAction:(id)sender
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];

    NSLog(@"Selected Cell is: %@",cell);
    NSLog(@"Selected row is: %d",[sender tag]);
}

您可以创建按钮分配

将按钮标记设置为tableView单元格的索引

[editButton setTag:indexPath.row];
在按钮

-(IBAction)editButtonAction:(id)sender
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];

    NSLog(@"Selected Cell is: %@",cell);
    NSLog(@"Selected row is: %d",[sender tag]);
}
用这种方法

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
添加此行:

  button.tag = indexPath.row;
int row = [sender tag];
在这个按钮动作中

 -(IBAction)editButtonAction:(id)sender
添加此行:

  button.tag = indexPath.row;
int row = [sender tag];
用这种方法

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
添加此行:

  button.tag = indexPath.row;
int row = [sender tag];
在这个按钮动作中

 -(IBAction)editButtonAction:(id)sender
添加此行:

  button.tag = indexPath.row;
int row = [sender tag];

实际上,代码工作正常..您选择了哪一行进行测试?是0吗? 尝试记录行和节

还应检查连接表的出口?以及该出口的参考是否发生了变化

否则代码对我来说运行良好

    NSLog(@"Row: %d\n ,Section :%d",indexPath.row,indexPath.section);;

实际上,代码工作正常..您选择了哪一行进行测试?是0吗? 尝试记录行和节

还应检查连接表的出口?以及该出口的参考是否发生了变化

否则代码对我来说运行良好

    NSLog(@"Row: %d\n ,Section :%d",indexPath.row,indexPath.section);;

你试过@Janusfiel答案吗…?嗨,伙计,它起作用了我测试了Janusfiel的答案。。。我认为你在其他方面犯了错误…你试过@janusfidel答案吗…?嗨,伙计,它起作用了我测试了janusfidel的答案。。。我认为你在其他方面犯了错误…@user1551089:wht是段数?@user1551089:wht是段数?