Objective c UIImageView initWithImage和访问索引处的元素时遇到问题
我收到了这个警告。我不确定我需要做什么。objective-C新手。似乎不喜欢这一行,并且在到达这一行后应用程序崩溃:Objective c UIImageView initWithImage和访问索引处的元素时遇到问题,objective-c,uiimageview,Objective C,Uiimageview,我收到了这个警告。我不确定我需要做什么。objective-C新手。似乎不喜欢这一行,并且在到达这一行后应用程序崩溃: UIImageView *tmp = [[UIImageView alloc] initWithImage:[data objectAtIndex:index]]; 我得到的警告是: /test/ViewController.m:186:89:指向整数转换的不兼容指针将“NSNumber*\uu strong”发送到“NSUInteger”类型的参数(也称为“unsigne
UIImageView *tmp = [[UIImageView alloc] initWithImage:[data objectAtIndex:index]];
我得到的警告是:
/test/ViewController.m:186:89:指向整数转换的不兼容指针将“NSNumber*\uu 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;
}
@界面视图控制器()
{
NSMutableArray*数据;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
UICollectionViewCell*单元格=(UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@“CollectionViewCell”forIndexPath:indexPath];
cell.backgroundColor=[UIColor whiteColor];
NSNumber*index=[dataobjectatindex:indexath.item];
用于(UIView*单元格中的子视图。子视图){
if([subview iskindof类:[UIImageView类]]){
[子视图从SuperView移除];
}
}
UIImageView*tmp=[[UIImageView alloc]initWithImage:[data objectAtIndex:index]];
[单元添加子视图:tmp];
返回单元;
}
方法objectAtIndex:
需要整数参数。传递index.integerValue
value.您的代码很奇怪,因为您正在访问索引和图像的同一数组!为什么数据数组不只是保存图像?然后您可以调用UIImage*image=[dataobjectatindex:indexPath.row]代码>
无论如何
对于您当前的问题,您只需更改:
NSNumber *index = [data objectAtIndex:indexPath.item];
UIImageView *tmp = [[UIImageView alloc] initWithImage:[data objectAtIndex:index]];
与:
我想指出的是,您应该使用名为imageView
的属性创建一个CustomCollectionViewCell
,这样您就可以更改图像,而不是每次都删除和添加imageView。加上只保存图像的数据
数组。
它看起来像:
- (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;
}
您还应该将数据
作为属性:
@property NSMutableArray *data;
并将其与self.data
一起使用,这样就很清楚它是一个属性。为什么data
数组似乎同时包含NSNumber
对象和UIImage
对象?
@property NSMutableArray *data;