【问题标题】:UIcollection view not getting call datasourcesUIcollectionview 没有得到调用数据源
【发布时间】:2015-03-01 04:03:43
【问题描述】:

您好,我正在从 xib 创建一个 UICollectionView。这是我的.h 课程

@interface ArtistViewController : <UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

@property (retain, nonatomic) IBOutlet UICollectionView *collectionview;

在我的.m ViewDidloadmethod 我这样做了

[collectionview setBackgroundColor:[UIColor clearColor]];

我的数据源和委托是这样的

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [dataArray count];
}



- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}





 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"CollectionViewCell";

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];


//artist = [arrayClass.mutArrayArtists objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
if([[dataArray objectAtIndex:indexPath.row] valueForKey:@"thumbImage"]!=nil){


    recipeImageView.image=[[dataArray objectAtIndex:indexPath.row] valueForKey:@"thumbImage"];
}
else
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *imageURL = [NSURL URLWithString:[[dataArray objectAtIndex:indexPath.row] valueForKey:@"thumbUrl"]];
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage *image = [UIImage imageWithData:imageData];
        dispatch_async(dispatch_get_main_queue(), ^{
            if(image!=nil){
                NSMutableDictionary *objectDict=[[NSMutableDictionary alloc] initWithDictionary:[dataArray objectAtIndex:indexPath.row]];
                [objectDict setObject:image forKey:@"thumbImage"];
                [dataArray replaceObjectAtIndex:indexPath.row withObject:objectDict];
            }

            recipeImageView.image= image;

        });
    });
}



UILabel *lblArtistname=(UILabel *)[cell viewWithTag:102];
lblArtistname.text=[[dataArray objectAtIndex:indexPath.row] valueForKey:@"name"];

lblArtistname.lineBreakMode=NSLineBreakByWordWrapping;
[lblArtistname setNumberOfLines:0];

CGSize maximumLabelSize = CGSizeMake(300,800);
CGSize expectedLabelSize = [[lblArtistname text] sizeWithFont:lblArtistname.font
                                            constrainedToSize:maximumLabelSize
                                                lineBreakMode:[lblArtistname lineBreakMode]];
CGRect newframe=lblArtistname.frame;
newframe.size.height=expectedLabelSize.height;
newframe.size.width=300;
[lblArtistname setFrame:newframe];


//UILabel *lblSongCount=(UILabel *)[cell viewWithTag:101];
// lblSongCount.text=artist.numberofSongs;
return cell;
}

但这并没有调用任何数据源方法。这是为什么。我该如何解决这个问题。请帮帮我。

谢谢

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    在.m ViewDidloadmethod 中添加这个

    [self.collectionview setDelegate:self];
    [self.collectionview setDataSource:self];
    

    并将第一行的错字更正为:

    @interface ArtistViewController :    UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
    

    【讨论】:

      【解决方案2】:

      在您的故事板(或您喜欢的视图控制器)中,将 UICollectionView 的数据源和委托出口设置为您的视图控制器。看起来你的 @interface 声明中有一个类型。

      @interface ArtistViewController : <UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
      

      应该是

      @interface ArtistViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
      

      【讨论】:

        猜你喜欢
        • 2015-09-10
        • 1970-01-01
        • 1970-01-01
        • 2017-12-02
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 2018-06-22
        • 1970-01-01
        相关资源
        最近更新 更多