[英]UITableViewCell can't add a subview
I want to add a Button in a TableView cell. 我想在TableView单元格中添加一个Button。
At the beginning, I was using a TableView and its cell from the Storyboard, and it was working. 开始时,我使用的是Storyboard中的TableView及其单元,并且可以正常工作。
I removed the TableView from the Storyboard, to add the TableView programmatically, and I created the cell in a XIB. 我从情节提要中删除了TableView,以编程方式添加了TableView,然后在XIB中创建了该单元格。
I did this to switch between a TableView displaying, and a CollectionView displaying. 我这样做是在显示TableView和显示CollectionView之间切换。
Then, I use the same code to create the button I want to display in the CollectionViewCell and the TableViewCell. 然后,我使用相同的代码创建要显示在CollectionViewCell和TableViewCell中的按钮。 It works for the Collection, not for the Table.
它适用于Collection,不适用于Table。 I added
checkBoxButton.backgroundColor = [UIColor blueColor];
我添加了
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. 我在单元格中有一个名为LIB_Nom的标签。 If I use [LIB_Nom addSubview:checkBoxButton], the button appears.
如果使用[LIB_Nom addSubview:checkBoxButton],则会出现该按钮。 (same for all elements of the cell.)
(对于单元的所有元素相同。)
Please help me, I'm raging for two hours. 请帮助我,我辛苦了两个小时。 Thanks :)
谢谢 :)
Here's my cellForRowAtIndexPath 这是我的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 在这里,我创建了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];
Try this: 尝试这个:
This way, the process of loading the xib creates and hooks everything up for you. 这样,加载xib的过程将为您创建并连接所有内容。 All you need to do is register the xib with the collectionView.
您需要做的就是在collectionView中注册xib。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.