我收到了这个警告。不确定我需要做什么修复。客观C新手。似乎不喜欢这条线和应用程序在到达此行后崩溃:
UIImageView *tmp = [[UIImageView alloc] initWithImage:[data objectAtIndex:index]];
警告我收到了:
/test/ViewController.m:186:89:指向整数转换的指针不兼容发送' NSNumber * __ strong'参数类型' NSUInteger' (又名' unsigned long')
ViewController.m文件:
@interface ViewController () <UICollectionViewDataSource_Draggable, UICollectionViewDelegate>
{
NSMutableArray *data;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = (UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
NSNumber *index = [data objectAtIndex:indexPath.item];
for (UIView *subview in cell.subviews) {
if ([subview isKindOfClass:[UIImageView class]]) {
[subview removeFromSuperview];
}
}
UIImageView *tmp = [[UIImageView alloc] initWithImage:[data objectAtIndex:index]];
[cell addSubview:tmp];
return cell;
}
答案 0 :(得分:0)
方法objectAtIndex:
需要NSUInteger参数。通过index.integerValue
值。
答案 1 :(得分:0)
您的代码很奇怪,因为您为索引和图像访问了相同的数组!为什么data
数组只是持有图像?然后你可以打电话给UIImage * image = [data objectAtIndex: indexPath.row];
反正...
根据您当前的问题,您只需要更改:
NSNumber *index = [data objectAtIndex:indexPath.item];
UIImageView *tmp = [[UIImageView alloc] initWithImage:[data objectAtIndex:index]];
使用:
NSNumber *indexNumber = [data objectAtIndex:indexPath.row];
UIImageView *tmp = [[UIImageView alloc] initWithImage:[data objectAtIndex:indexNumber.intValue];
我想指出您应该使用名为CustomCollectionViewCell
的属性创建imageView
,以便您可以更改图像,而不是每次都删除和添加imageView。加上只保存图像的data
数组。
它看起来像是:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue or initialize a new custom cell
CustomCollectionViewCell *cell = (UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
// Configure the cell
cell.backgroundColor = [UIColor whiteColor];
cell.imageView.image = [data objectAtIndex:indexPath.row];
return cell;
}
您还应该将data
作为财产:
@property NSMutableArray *data;
并将其与self.data
一起使用,以便明确表明它属于属性。