在我的表格视图中,我在每个单元格中都有一个按钮
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 有什么想法吗?和任何代码
答案 0 :(得分:1)
替代解决方案:
.
.
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);
}
答案 1 :(得分:0)
您创建按钮分配
将button标记设置为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]);
}
答案 2 :(得分:0)
在这种方法中..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
添加以下行:
button.tag = indexPath.row;
在此按钮操作中,
-(IBAction)editButtonAction:(id)sender
添加以下行:
int row = [sender tag];
答案 3 :(得分:0)
实际上代码工作得很好..你选择哪一行进行测试?是0吗? 尝试记录行和部分。
还要检查已连接的桌子的插座?是否在该插座的任何地方更改了参考
否则代码运行正常
NSLog(@"Row: %d\n ,Section :%d",indexPath.row,indexPath.section);;