所以,我有这段代码
UIButton *accessoryButton = [[UIButton alloc] init];
UIImage *accImage = [UIImage imageNamed:@"ShareAccButton.png"];
[accessoryButton setImage:accImage forState: UIControlStateNormal];
UIView *accView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 40, 15)];
[accView addSubview:accessoryButton];
cell.accessoryView = accView;
在此,我试图将按钮作为表视图单元的附件。这是行不通的,我想知道如何才能成功完成此操作。另外,如何将这些按钮连接到功能上?
最佳答案
我想你错过了按钮的框架。只需使用框架初始化按钮即可。
正如jamihash所说,您可以将按钮直接分配为单元格的annexView。我希望以下代码可以工作。
UIButton *accessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIImage *accImage = [UIImage imageNamed:@"ShareAccButton"];
[accessoryButton setImage:accImage forState: UIControlStateNormal];
[accessoryButton addTarget:self action:@selector(yourMethodName) forControlEvents:UIControlEventTouchUpInside];
[cell setAccessoryView:accessoryButton];