Reputation: 288
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];
Upvotes: 0
Views: 729
Reputation: 3347
Try this:
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.
Upvotes: 1