Ios Objective-C中的数组出现问题,希望重用数组,但无法删除以前的值
我在以下函数中使用的数组有问题Ios Objective-C中的数组出现问题,希望重用数组,但无法删除以前的值,ios,objective-c,arrays,xcode,uicollectionview,Ios,Objective C,Arrays,Xcode,Uicollectionview,我在以下函数中使用的数组有问题 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 使用UICollectionView调用此函数。给我带来问题的代码如下: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionV
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
使用UICollectionView调用此函数。给我带来问题的代码如下:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionCell" forIndexPath:indexPath];
NSInteger *numEmptyCells = (int*)([self getNumberFirstDayOfMonth:[self getNameOfFirstMonthDay:datePickerDateString]]);
if (!tempMutableArray)
{
tempMutableArray = [[NSMutableArray alloc] init];
}
[tempMutableArray removeAllObjects];
tempMutableArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [self getNumberFirstDayOfMonth:[self getNameOfFirstMonthDay:datePickerDateString]]; i++)
{
[tempMutableArray addObject:[NSString stringWithFormat:@"%d", i]];
//[tempMutableArray addObject:@"empty"];
}
[tempMutableArray addObjectsFromArray:monthDatesObjectsArray];
if (cell != nil)
{
if ([tempMutableArray containsObject:[NSString stringWithFormat:@"%d", indexPath.row]])
//if ([tempMutableArray containsObject:[NSString stringWithFormat:@"%@", @"empty"]])
{
//[cell updateCollectionCell:nil cellImage:nil];
[cell setHidden:true];
}
else
{
[cell updateCollectionCell:[NSString stringWithFormat:@"%d", (indexPath.row - (int)numEmptyCells + 1)] cellImage:nil];
}
}
return cell;
//return nil;
}
具有随机数,这会弄乱其余单元格,因为如果这些值的值等于单元格中的其他值,则这些单元格将被[cell setHidden:true]
禁用。如何确保每次单元格填充值时不会得到随机值?请参阅我提供的一些截图链接。如有任何建议,我们将不胜感激。谢谢
屏幕截图:真正有助于理解对象及其生命周期。只有在下一行为同一变量创建一个新的
NSMutableArray
时,才从NSMutableArray
中删除所有对象是没有意义的。未经许可,无法访问屏幕截图-而且您将init
ingtemmutablearray
两次。还请注意,每次需要显示单元格时都会调用此方法,因此如果结果可以重用,则在单元格内进行对象创建/操作并不理想。谢谢回复。我已尝试将tempMutableArray
分配到cellForItemAtIndexPath
之外,但我想要的结果只有在collectionview第一次加载单元格时才会出现。第二次和for循环不起作用,而是在单元格开始按顺序显示1、2、3之前,我检查循环添加到数组中的是随机数。因此,如果其他单元格具有相同的值,它们将被[cell setHidden:true]
禁用。为什么for循环只在第一次工作?有没有办法正确清除阵列?我不知道为什么它不起作用。我修复了上面的链接。
for (int i = 0; i < [self getNumberFirstDayOfMonth:[self getNameOfFirstMonthDay:datePickerDateString]]; i++)
{
[tempMutableArray addObject:[NSString stringWithFormat:@"%d", i]];
//[tempMutableArray addObject:@"empty"];
}