1

我的代码是:

- (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]]];
4

2 回答 2

0

我有你的解决方案尝试这样:

 cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nameImage.jpg"]];
于 2013-10-25T20:37:40.053 回答
0

你做错了什么viewDidLoad

您唯一需要做的就是为您的 UICollectionView 设置颜色,以便像这样更改您的代码,例如:

[collectionView setBackgroundColor:[UIColor redColor]];

假设您的数组tmp是一个图像名称数组(如“myImage.png”),并在您的结束之前重新加载您的集合视图viewDidLoad

[collectionView reloadData];
于 2013-10-25T15:56:40.660 回答