Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UICollectionViewCell中分配UILabel_Ios_Objective C_Uicollectionviewlayout - Fatal编程技术网

Ios 在UICollectionViewCell中分配UILabel

Ios 在UICollectionViewCell中分配UILabel,ios,objective-c,uicollectionviewlayout,Ios,Objective C,Uicollectionviewlayout,我制作的应用程序具有集合视图和不同的纵向和横向布局,我为每个单元格分配UIlabel 我就是这样做的 UILabel *scheduleCellLabel = [[UILabel alloc] init]; scheduleCellLabel.textAlignment = NSTextAlignmentCenter; scheduleCellLabel.frame = CGRectMake(0, 0,scheduleCell.bounds.size.width, scheduleCell.bo

我制作的应用程序具有集合视图和不同的纵向和横向布局,我为每个单元格分配UIlabel

我就是这样做的

UILabel *scheduleCellLabel = [[UILabel alloc] init];
scheduleCellLabel.textAlignment = NSTextAlignmentCenter;
scheduleCellLabel.frame = CGRectMake(0, 0,scheduleCell.bounds.size.width, scheduleCell.bounds.size.height);
[scheduleCell addSubview:scheduleCellLabel];
问题是,每次方向更改时,我都会重新加载集合视图,但旧单元格仍保留在集合视图中。我试图在装载时从电池上取下标签,但这不是问题所在

这就是它的样子:


提前感谢。

删除并添加cellForItemAtIndexPath中的标签。首先检查标签是否在单元格中,如果是,请将其从superview中删除,然后创建新标签并将其添加到其contentview中。

可能,您在cellForRowAtIndexPath方法中分配了uilabel,并在重新加载collectionView时反复出现。 要解决您的问题,只需将标签定义为属性,并在viewDidLoad方法中分配它,然后在cellForRowAtIndexPath方法中执行所需操作。希望这会有所帮助

例如,这是您的头文件:

@property (retain,nonatomic) IBOutlet UILabel *label;
这是implemantation文件的viewDidLoad方法:

    _label = [[UILabel alloc] init];

在我看来,除非您需要,否则将子视图添加代码放在cellForRowAtIndexPath中是不好的做法;例如,如果某些单元格具有子视图,而某些单元格不基于indexPath。您应该在IB中创建单元格,或者对单元格进行子类化,并在单元格的init方法中添加任何子视图。这样,您不必在重新添加视图之前删除它们,也不必在添加视图之前检查它们是否存在;这只会使您的CellForRowatineXpath方法变得复杂。

您的代码在哪里?如果该标签在每个单元格中,那么我建议将代码放在自定义单元格的init方法中(或在IB中执行),而不是放在cellForItemAtIndexPath中(如果您正在这样做的话)。对不起,我忘了提到这一点。这段代码在cellForItemAtIndexPath方法中。我试过了,但这不是问题所在。标签在新加载后自动释放。是的,你说得对。这不是最好的解决办法。我尝试另一种方式。非常感谢。