0

I want to add a Button in a TableView cell.

At the beginning, I was using a TableView and its cell from the Storyboard, and it was working.

I removed the TableView from the Storyboard, to add the TableView programmatically, and I created the cell in a XIB.

I did this to switch between a TableView displaying, and a CollectionView displaying.

Then, I use the same code to create the button I want to display in the CollectionViewCell and the TableViewCell. It works for the Collection, not for the Table. I added checkBoxButton.backgroundColor = [UIColor blueColor]; and I don't see the blue square.

NSLog(@"Cell subviews before %i", [cell.contentView.subviews count]);

    UIButton *checkBoxButton = d[@"Button"];
    if(checkBoxButton){
        checkBoxButton.frame = CGRectMake(850,60, 50, 50);
        checkBoxButton.backgroundColor = [UIColor blueColor];
        [cell.contentView addSubview:checkBoxButton];
    }

NSLog(@"Cell subviews after %i", [cell.contentView.subviews count]);
    return cell;

I've a label named LIB_Nom in the cell. If I use [LIB_Nom addSubview:checkBoxButton], the button appears. (same for all elements of the cell.)

Please help me, I'm raging for two hours. Thanks :)

Here's my cellForRowAtIndexPath

        CellIdentifier = @"personne_Table";
        CELL_Personne_Table *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        CLASS_Contact* unContact = [tab_Personnes objectAtIndex:[indexPath row]];
        NSDictionary* d = [self loadCellPersonne:indexPath];

        cell.LIB_Nom.text = d[@"Nom"];
        cell.LIB_Contact.text = d[@"Contact"];
        cell.LIB_Adresse.text = d[@"Adresse"];
        cell.LIB_Adresse_2.text = d[@"Adresse2"];
        cell.LIB_CP_Ville.text = d[@"CP_Ville"];
        cell.LIB_ID.text = d[@"ID"];
        cell.LIB_Tel_1.text = d[@"Tel"];
        cell.LIB_Tel_2.text = d[@"Tel_2"];
        cell.LIB_Email.text = d[@"Email"];
        cell.LIB_DateCreation.text = d[@"DateCreation"];
        cell.IMG_Rdv.hidden = [unContact Get_HideRdV];
        cell.IMG_Pays.image = d[@"IMG_Pays"];
        cell.IMG_Contact.image = d[@"IMG_Contact"];
        cell.LIB_Visite.text = d[@"Visite"];

        for(UIView* v in cell.subviews)
        {
            NSLog(@"Cell Subview %p", v);
        }
        UIButton *checkBoxButton = d[@"Button"];
        if(checkBoxButton){
            checkBoxButton.frame = CGRectMake(0,0, 50, 50);
            checkBoxButton.backgroundColor = [UIColor blueColor];

            NSLog(@"content View %p", cell.contentView);
            cell.contentView.backgroundColor = [UIColor redColor];
            [cell.contentView addSubview:checkBoxButton];
        }
        NSLog(@"-------------------");

        return cell;

Here, I create the Tableview

TABLE_Contacts = [[UITableView alloc] initWithFrame:f];
        TABLE_Contacts.backgroundColor = [UIColor clearColor];
        TABLE_Contacts.delegate = self;
        TABLE_Contacts.dataSource = self;
        [TABLE_Contacts registerNib:[UINib nibWithNibName:@"CELL_Personne_Table" bundle:nil] forCellReuseIdentifier:@"personne_Table"];
        [self.view addSubview:TABLE_Contacts];
Verdant
  • 288
  • 1
  • 10
  • Wow, your x value is 850?? Is your tableview in landscape on an iPad? – Lyndsey Scott Nov 30 '14 at 17:57
  • Yep :) And I tried with a lot of values ;) – Verdant Nov 30 '14 at 17:58
  • Oh, OK. Could you post your entire cellForRowAtIndexPath method? – Lyndsey Scott Nov 30 '14 at 17:58
  • So in your CELL_Personne_Table class, what code are you using to "addSubview"? Are you adding the elements like [cell addSubview...] or [cell.contentView addSubview...] – Lyndsey Scott Nov 30 '14 at 18:04
  • I posted my code. I use cell.contentView. If I use cell.addsubview, I can't clic the button. But the real problem seems to be that contentView is not displayed. – Verdant Nov 30 '14 at 18:06
  • No, I meant in your CELL_Personne_Table class where you're adding LIB_Nom for example. – Lyndsey Scott Nov 30 '14 at 18:07
  • And are you sure d[@"Button"] is set? Can you put a log or breakpoint in the conditional to double check? – Lyndsey Scott Nov 30 '14 at 18:08
  • I added all the elements in the XIB file. – Verdant Nov 30 '14 at 18:08
  • I'm sure it's set. I NSLogged it, and the console logs contentView's value. – Verdant Nov 30 '14 at 18:09
  • My questions might seem tedious, but I'm asking for a reason -- If you look at this Apple doc you can see how the contentView is actually subview of the cell: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html So if the elements in your .xib are subviews of the cell & not the content view, but then you're adding your button to the contentView, it could be covered up by those elements in the cell's superview. So when you said "If I use cell.addsubview, I can't clic the button", was the button actually showing? – Lyndsey Scott Nov 30 '14 at 18:17
  • Yep it was. I forgot to tell that, sorry :) – Verdant Nov 30 '14 at 18:19
  • OK, first things first, keep your code exactly the way it was with [cell.contentView addSubview:checkBoxButton]. Just try adding this line after you've added the subview: [cell.contentView bringSubviewToFront:checkBoxButton]; – Lyndsey Scott Nov 30 '14 at 18:31
  • I found a thread addressing the same issue you're having here: http://stackoverflow.com/questions/18848689/button-in-uitableviewcell-not-responding-under-ios-7 And I think the `bringSubviewToFront:` answer is the most likely solution in this case, but there are plenty more suggestions at that link. That particular user suggests using `[cell bringSubviewToFront:checkBoxButton];` though which is probably more correct than my code since it seemed to have worked for so many people... – Lyndsey Scott Nov 30 '14 at 18:39

1 Answers1

1

Try this:

  • In your xib, create all the subviews you may want to use at runtime and hide them.
  • Place them under the contentView (in the xib)
  • Connect them into your custom cell class by ctrl-dragging into your code.
  • At runtime, just hide/unhide the appropriate views in response to the action.

This way, the process of loading the xib creates and hooks everything up for you. All you need to do is register the xib with the collectionView.

Anna Dickinson
  • 3,307
  • 24
  • 43
  • I'll do this 'cause I'm really bored of trying to understand the un-understandable. ^^ Thx :) PS : The Collection works. The Tableview doesn't. PS : There is not contentView in the XIB. I NSLogged the contentView pointer -> not nil. I NSLogged all cell.subviews -> no contentView. And I tried contentView.backgroundColor = [UIColor redColor] -> Nothing happens. Strange. All works perfectly in the Collection – Verdant Nov 30 '14 at 17:53
  • Yeah, sometimes it's best not to fight the magic stuff the SDK does behind the scenes. (Btw, can you accept my answer? :-) click the check) – Anna Dickinson Nov 30 '14 at 17:57