我正在开发具有每个表格视图单元格中动态数量按钮功能的应用程序。按钮数量因行而异。如何获取表格视图单元格上的按钮动态按钮的按钮事件
我想给图像一个按钮来点击。但我无法得到它,所以任何人都可以帮助我改变所选按钮的图像。
我正在开发具有每个表格视图单元格中动态数量按钮功能的应用程序。按钮数量因行而异。如何获取表格视图单元格上的按钮动态按钮的按钮事件
我想给图像一个按钮来点击。但我无法得到它,所以任何人都可以帮助我改变所选按钮的图像。
您应该为您的按钮添加标签,并在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中为其分配indexPath.row
。
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:@"Button" forState:UIControlStateNormal];
[yourButton addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
yourButton.tag=indexPath.row;
yourButton.frame = CGRectMake(5, 5, 100, 30);
[cell addSubview:yourButton];
之后,定义buttonSelected:
方法。
-(void)buttonSelected:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(@"buttonSelectd: %d",button.tag);
//implement your code what you want.
}
我认为这会对您有所帮助。
感谢prasad为你的答案我知道它工作正常,但我的问题是每个单元格上的按钮数是不同的手段(单元格1有2个按钮,单元格2 6按钮)按钮的数量不固定,所以我有麻烦,这样做这里是我的示例代码添加按钮单元格 – user1579258 2012-08-17 05:01:08
for(int i = 0; i <[Array count]; i ++) { optionButton = [[UIButton alloc] init]; optionButton = [UIButton buttonWithType:UIButtonTypeCustom]; optionButton.frame = CGRectMake(5,80,22,18); [optionButton addTarget:self action:@selector(buttonPresssed :) forControlEvents:UIControlEventTouchUpInside]; optionButton.tag = i; [cell addSubview:optionButton]; } – user1579258 2012-08-17 05:07:58
按钮的数量取决于阵列并发,所以我无法追踪哪个按钮被点击,还有一件事我必须一起收集选择按钮值,所以请你可以告诉我如何做到这一点。预先感谢 – user1579258 2012-08-17 05:09:55
请显示相关的代码,你已经尝试过:-) – 2012-08-11 05:17:54