[英]UITableViewCell can't add a subview
我想在TableView单元格中添加一个Button。
开始时,我使用的是Storyboard中的TableView及其单元,并且可以正常工作。
我从情节提要中删除了TableView,以编程方式添加了TableView,然后在XIB中创建了该单元格。
我这样做是在显示TableView和显示CollectionView之间切换。
然后,我使用相同的代码创建要显示在CollectionViewCell和TableViewCell中的按钮。 它适用于Collection,不适用于Table。 我添加了checkBoxButton.backgroundColor = [UIColor blueColor];
我看不到蓝色方块。
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;
我在单元格中有一个名为LIB_Nom的标签。 如果使用[LIB_Nom addSubview:checkBoxButton],则会出现该按钮。 (对于单元的所有元素相同。)
请帮助我,我辛苦了两个小时。 谢谢 :)
这是我的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;
在这里,我创建了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];
尝试这个:
这样,加载xib的过程将为您创建并连接所有内容。 您需要做的就是在collectionView中注册xib。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.