目标C> UITableViewCell按钮没有令人沮丧

时间:2014-09-17 17:59:43

标签: objective-c uitableview uibutton

我在此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视图,以便直接在contentView中创建按钮,这没有任何效果。仍然看起来唯一被识别的手势是用于滚动表格的手势。点击按钮似乎不会改变文本的颜色,或者表示按下按钮并且没有创建日志条目。

编辑2:添加了cellForRowAtIndexPath以粘贴bin http://pastebin.com/WXezfW96

6 个答案:

答案 0 :(得分:1)

我最近遇到过类似的问题,我很难理解。我想出的解决方案是设置按钮的边界,并检查UIControlEventTouchDown而不是UIControlEventTouchUpInside。

答案 1 :(得分:1)

您是否尝试过使用cancelsTouchesInView?如果您添加了点击手势,我认为点击手势或表格单元格中的点按会覆盖按钮中的触摸。尝试使用此代码:

UIGestureRecognizer *tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(yourAction)];
    tap.cancelsTouchesInView = NO;
    tap.delegate = self;
    [yourButton addGestureRecognizer:tap];

但请不要忘记在.h文件中添加<UIGestureRecognizerDelegate>

但我同意迈克。创建表格单元格时,使其简短。我认为你可以使用自定义单元子类和xib文件。

我也是客观C的新手,所以如果我说错了,我很抱歉,我只是想帮忙。

答案 2 :(得分:0)

UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];

此更改UIButtonTypeCustomUIButtonTypeRoundedRect

答案 3 :(得分:0)

斯科特,您可能想尝试我着名的故障排除技术:

  1. 确定按钮操作是否是根本原因:
  2.     UIImage *image = [UIImage imageNamed:@"image.png"];
        [button setBackgroundImage:image forState:UIControlStateHighlighted|UIControlStateSelected]
    

    1. 而不是:
    2. -(void)showAppointment:(UIButton*)sender { 
      

      -(void)showAppointment:(id*)sender {
      

      1. 使用断点。

      2. 如果以上工作都不考虑重构您看起来有点混乱的代码。

答案 4 :(得分:0)

嘿,我检查了你写的代码,工作正常。你能否检查一下你的cellForRowAtIndexPath中是否有类似下面的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *Identifier = @"Identifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];

    if(cell == nil)

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];

    }

        UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];

        sched_button_a1.frame = CGRectMake(0, 0, 110, 52);

        CGRect frame_a1 = sched_button_a1.frame;


        [sched_button_a1 addTarget:self action:@selector(showAppointment:) forControlEvents:UIControlEventTouchUpInside];

        [sched_button_a1 setTitle:@"Your Text" forState:UIControlStateNormal];

        sched_button_a1.layer.backgroundColor = [UIColor redColor].CGColor;

        [cell.contentView addSubview:sched_button_a1];


    return cell;

}

由于您没有保留整个cellForRowAtIndexPath代码,因此我无法进一步调试。希望这可以帮助您解决问题

答案 5 :(得分:0)

由于您使用的是UIButtonTypeCustom的按钮类型,因此您需要设置UIControlStateHighlighted和UIControlStateSelected的属性,以便获得想要按下按钮所需的效果。 您可以使用以下UIButton方法之一更改突出显示和选定状态的标题颜色或背景图像:

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

作为关于cellForRowAtIndexPath中代码的补充说明,您不应该从方法中获取sql中的数据,这会破坏您的表视图性能,获取之前需要的所有数据并将其存储起来在数组中的对象中,然后在cellForRowAtIndexPath方法中,您只需更新行的对象中的值标签。

相关问题