我有一个 UITableView ,我希望用户能够在该部分的标题中单击两个按钮,一个用于添加新记录,另一个用于编辑它们,所以我首先尝试使用tableView viewForHeaderInSection
并tableView heightForHeaderInSection
像这样:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 360, 44)];
UIToolbar* tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 360, 44)];
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onAddVehicleInterest:)];
UIBarButtonItem* editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(onEditVehiclesInterest:)];
[tb setItems:[NSArray arrayWithObjects:spacer, addBtn, editBtn, nil]];
[v addSubview:tb];
return v;
}
- (IBAction)onAddVehicleInterest: (id) sender {
NSLog(@"Add");
}
- (IBAction)onEditVehiclesInterest:(id)sender {
NSLog(@"Edit");
}
当我运行该应用程序时,我可以正确地看到三个 UIBarButtonItems,并且可以单击它们,但是永远不会调用 NSLog 行。所以我UIToolbar
直接在 nib 文件中添加了一个,将 3 个UIBarButtonItem
控件添加到它,并将 and 绑定onAddVehicleInterest
到onEditVehiclesInterest
相应的按钮。但这也不起作用...
因此,作为最后一个测试,我UIButton
在 nib 中添加了一个并将其 Touch Up Inside 事件绑定到其中一个方法,并且按预期工作。所以我很困惑。我究竟做错了什么?