Not getting events from controls in the accessoryview in a UITableViewCell

Discussion in 'Mac Programming' started by caldwelljason, Jul 9, 2008.

  1. caldwelljason macrumors member

    Joined:
    Jul 9, 2008
    #1
    I want to have a couple of controls on the right side of my cell. Specifically, I need a UILabel and a UIButton. When the button is touched, I want to update the label.

    If I remove the label and assign the button as the accessoryView (cell.accessoryView = button), it works properly and I get my events.

    If I create a UIView, assign that as the accessoryView, and add the label and button as subViews of the accessoryView, I don't get any events for the button.

    UIFont *font = [UIFont systemFontOfSize:12];
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.text = @"some text";
    label.frame = CGRectMake(210, 10, 70, 25);
    label.textAlignment = UITextAlignmentRight;
    label.font = font;

    [cell.accessoryView addSubview:label];

    // Add the button
    UIButton* accessoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [accessoryButton addTarget:self action:mad:selector( touchedRow: ) forControlEvents:UIControlEventTouchUpInside];
    [accessoryButton setFrame:CGRectMake(281, 10, 30, 25)];
    [accessoryButton setImage:[UIImage imageNamed:mad:"TrackingDot.png"] forState:UIControlStateNormal];
    [accessoryButton setUserInteractionEnabled:YES];

    [cell.accessoryView addSubview:accessoryButton];

    I tried enabling user interaction on both the table row and the accessoryView itself, to no avail. I also tried assigning the event handler to the accessoryView, rather than the button, itself. But, of course, UIView doesn't respond to that message.

    I am begging for any suggestions. I have been banging my head on this wall for a couple of days and can't seem to get past it. :confused:
     
  2. caldwelljason thread starter macrumors member

    Joined:
    Jul 9, 2008
    #2
    Got it fixed

    OK. I'm not sure why; but if I add my subviews to the UITableViewCell itself, rather than to the accessoryView of the cell, it works.

    So I replaced

    [cell.accessoryView addSubview:label];

    with
    [cell addSubView:label];

    and now everything is working fine. I can explicitly set the location of the label and button, so it still appears the same either way. I am now effectively ignoring the predefined subviews of the UITableViewCell and making everything a direct child of the cell, itself.

    It still seems like those items ought to be in the accessoryView. But I'll take working over not working...;)

    Hope this helps someone else...:D
     

Share This Page