【发布时间】:2014-11-30 17:31:11
【问题描述】:
我想在 TableView 单元格中添加一个按钮。
一开始,我在 Storyboard 中使用 TableView 及其单元格,它正在工作。
我从 Storyboard 中删除了 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];
【问题讨论】:
-
哇,你的x值是850??你的桌面视图在 iPad 上是横向的吗?
-
是的 :) 我尝试了很多值 ;)
-
哦,好的。你能发布你的整个 cellForRowAtIndexPath 方法吗?
-
那么在您的 CELL_Personne_Table 类中,您使用什么代码来“addSubview”?您是否添加了 [cell addSubview...] 或 [cell.contentView addSubview...] 等元素
-
我发布了我的代码。我使用 cell.contentView。如果我使用 cell.addsubview,我无法单击按钮。但真正的问题似乎是 contentView 没有显示出来。
标签: ios objective-c uitableview