2

私はiOSプログラミングに非常に慣れておらず、json応答からUICollectionViewアイテムを設定しようとしています。

これが私のコードです:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self downloadJSONData];

    /* uncomment this block to use subclassed cells */
    [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"];
    /* end of subclass-based cells block */

    // Configure layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:CGSizeMake(150, 150)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.collectionView setCollectionViewLayout:flowLayout];
    [self.collectionView reloadData];
}

-(void) downloadJSONData {
    NSURL *serviceUrl = [[NSURL alloc]initWithString:@"http://myurlhere.com/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:serviceUrl];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [conn start];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.data = [[NSMutableData alloc]init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)responsedata {
    [self.data appendData:responsedata];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error : %@", error);
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Succeeded! Received %d bytes of data",[self.data length]);
    NSString *txt = [[NSString alloc] initWithData:self.data encoding: NSASCIIStringEncoding];
    NSLog(@"response data %@", txt);
    self.dictionaryData = [txt JSONValue];
}

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

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    //NSMutableArray *sectionArray = [self.dataArray objectAtIndex:section];
    self.jsonArray = [self.dictionaryData objectForKey:@"images"];
    return [self.jsonArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // Setup cell identifier
    static NSString *cellIdentifier = @"cvCell";

    /*  Uncomment this block to use nib-based cells */
    // UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    // UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    // [titleLabel setText:cellData];
    /* end of nib-based cell block */

    /* Uncomment this block to use subclass-based cells */
    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
    NSString *cellData = [data objectAtIndex:indexPath.row];
    [cell.titleLabel setText:cellData];
    /* end of subclass-based cells block */

    // Return the cell
    return cell;
}

コードを実行すると、何も表示されません。私が欲しいのは、アイテムを表示し、数量が配列の数と等しいことです。そしてもう1つ、画像を非同期でコレクションビューにロードする方法についての提案はありますか?

英語でごめんなさい。誰かがこれについて私を助けてくれたら嬉しいです。

ありがとう。乾杯!

4

1 に答える 1

1

メソッドにconnectionDidFinishLoading次を追加します。

[self.collectionView reloadData];

Asyn画像の読み込みについては、次のいずれかを確認できます。

  1. SDWebImage
  2. AsyncImageView
  3. LazyTableImages -UITableView用ですが、UICollectionViewにも同じ概念を使用できます
于 2013-02-28T03:39:23.920 に答える