gpt4 book ai didi

objective-c - 合适的 View 按钮 Action

转载 作者:行者123 更新时间:2023-11-29 13:28:26 24 4
gpt4 key购买 nike

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

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];

按钮 Action 是这样的

-(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);
}

当我在表格 View 中单击按钮时,选定的行是总是给出 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);
}

关于objective-c - 合适的 View 按钮 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12471641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com