我的代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(30, 20, 260, 270) collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[collectionView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:collectionView];
string1=[NSString stringWithFormat:url];
NSURL *jsonUrl = [NSURL URLWithString:string1];
NSData *data = [NSData dataWithContentsOfURL:jsonUrl];
UIImage *image1 =[[UIImage imageWithData:data]autorelease];
set=[NSArray arrayWithObject:image1];
tmp= [[NSMutableArray alloc] init];
for (int i=1; i<[set count]; i++)
{
[tmp addObject:[set objectAtIndex:i+1]];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [tmp count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[tmp objectAtIndex:indexpath.row]]] ;
cell.backgroundColor=[UIColor clearColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(80, 80);
}
我觉得这个动作不起作用或者当我伤害到东西时
cell.backgroundview =[UIImageView alloc]initwithimage:[UIImage imageNamed:[tmp objectAtIndex:indexPath.row]]];