Ios 如何使用viewWithTag获取按钮引用
我在故事板中创建了一个Ios 如何使用viewWithTag获取按钮引用,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在故事板中创建了一个动态原型单元,这个单元包含一个UILabel和两个uibutton 行数取决于我从web服务获取的数据 我不想给这个单元格划分子类 我正在使用此代码获取单元格内的控件引用[cell view with tag:0] 对于tap事件,我使用以下代码 [cell.button addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside]; 问题是如何检测点击了哪
动态原型单元,这个单元包含一个UILabel
和两个uibutton
行数取决于我从web服务获取的数据
我不想给这个单元格划分子类
我正在使用此代码获取单元格内的控件引用[cell view with tag:0]
对于tap事件,我使用以下代码
[cell.button addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
问题是如何检测点击了哪一行的按钮?在cellForRow方法中,您将标签添加到按钮,如下所示:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"identif"];
cell.button.tag = indexPath.row; // the tag of the button will be the index of the cell;
}
建议对标记使用某种公式,例如:100+indexath.row,因为应用程序中每个视图的标记最初都是0
之后,在按钮选择器中只需执行以下操作:
- (void)myMethod:(id)sender {
UIButton *button = (UIButton *)sender;
NSLog(@"%ld",(long)buttton.tag);
}
在cellforRowAtIndexPath中写入此内容
UIButton *button = (UIButton *)[cell viewWithTag:100]
[button setTag:indexPath.row];
在你的行动方法中写下
-(void)myMethod:(UIButton *)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
// indexPath contains clicked row indexpath
}
请再次查看我的问题。谢谢。我说的是对的。在方法中,只需获取标记。检查我的编辑否你错了,因为我说我不想子类化。如果你不将UITableViewCell子类化,你就不能从情节提要中进行操作,你在哪里添加操作和输出?在哪门课上?我在问题中也提到了这一点,这就是为什么我说要正确阅读我的问题。请再次阅读我的问题。@S.J请解释我,您没有对表视图单元格进行子类化,因此无法为按钮创建插座连接(因为连接不能将原型对象作为其目标)但是你是如何引用像cell.button一样的的?????