uts*_*iem 5 iphone objective-c uibutton uitableview
我想在一个按钮中添加一个按钮UITableViewCell.这是我的代码:`
if (indexPath.row==2) {
UIButton *scanQRCodeButton = [[UIButton alloc]init];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:@"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
}`
Run Code Online (Sandbox Code Playgroud)
现在,当我运行应用程序时,我只看到一个空行!有任何想法吗 ?
Kev*_*Low 13
虽然将它放在单元格的contentView中是很自然的,但我确信这不是问题(实际上,在过去,我从来没有在contentView中正确显示子视图,所以我总是使用单元格).
无论如何,问题涉及到开始创建按钮时的前三行.前两行很好,但代码停止使用:
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Run Code Online (Sandbox Code Playgroud)
buttonWithType:实际上是一种创建按钮的便捷方法(它就像一个紧凑的alloc-init).因此,它实际上"消除"了你过去的两行(你基本上创建了两次按钮).您只能使用init或buttonWithType:作为相同的按钮,但不能同时使用两者.
UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:@"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
Run Code Online (Sandbox Code Playgroud)
这将工作(请注意,如果需要,您可以使用cell.contentView).如果您没有使用自动引用计数(ARC),我想提一下,您不必在内存管理方面做任何事情,因为buttonWithType:返回一个自动释放的按钮.
UIButton *deletebtn=[[UIButton alloc]init];
deletebtn.frame=CGRectMake(50, 10, 20, 20);
deletebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setImage:[UIImage imageNamed:@"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn addTarget:self action:@selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deletebtn];
Run Code Online (Sandbox Code Playgroud)
要么
//在项目UIButton + EventBlocks中下载类并导入
UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setFrame:CGRectMake(170,5, 25, 25)];
deletebtn.tag=indexPath.row;
[deletebtn setImage:[UIImage imageNamed:@"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn setOnTouchUpInside:^(id sender, UIEvent *event) {
//Your action here
}];
[cell addSubview:deletebtn];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41133 次 |
| 最近记录: |