我在此 tableView 的 cellForRowAtIndexPath 部分中创建了以下按钮:
UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];
sched_button_a1.frame = CGRectMake(0, 0, 110, 52);
CGRect frame_a1 = sched_button_a1.frame;
// Resize height based on length of appointment
frame_a1.size.height = heightCalc;
sched_button_a1.frame = frame_a1;
[sched_button_a1 addTarget:self action:@selector(showAppointment:) forControlEvents:UIControlEventTouchUpInside];
[sched_button_a1 setTitle:[NSString stringWithFormat:@"APPT: %@", APPT_ID ] forState:UIControlStateNormal];
sched_button_a1.layer.backgroundColor = [UIColor redColor].CGColor;
[sched_a1 addSubview:sched_button_a1];
如您所见,它正在调用 showAppointment,目前只是:
-(void)showAppointment:(UIButton*)sender {
NSLog(@"Button pushed");
}
showAppointment 也在这里定义:
-(void)showAppointment:(UIButton*)sender;
该按钮显示良好,并获得正确的文本和背景,并且似乎位于前台,但是,当我单击它时,没有任何反应,并且按钮看起来甚至没有被按下。
我做错了什么或遗漏了什么?
编辑:我已经返回并删除了 sched_a1 View ,以便直接在 contentView 中创建按钮,但这没有效果。看起来唯一被识别的手势仍然是用于滚动表格的手势。点击该按钮似乎不会更改文本的颜色,也不会以其他方式指示该按钮已被按下并且不会创建任何日志条目。
编辑2:添加cellForRowAtIndexPath到粘贴箱 http://pastebin.com/WXezfW96
最佳答案
我最近遇到了类似的问题,我很难理解。我想出的解决方案是设置按钮的边界,并检查 UIControlEventTouchDown 而不是 UIControlEventTouchUpInside。
关于objective-c - Objective C > UITableViewCell 按钮不按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25897326/