将表视图控制器更改为集合视图控制器

时间:2013-09-04 18:01:11

标签: ios objective-c json

我有一个表视图控制器,我想将其更改为Collection View Controller,我的应用程序使用JSON获取其信息。

我已经在Storyboard中创建了一个Collection View Controller。我的视图Controller被称为“UpcomingReleasesViewController”,我有一个名为“UpcomingReleaseCell”的UICollectionViewCell。在我的故事板中,我有一个链接到Collection Cell的标签叫做“release_name”。

我想转发我在TVC中的代码,但我在更新时遇到了一些问题。

我添加到UpcomingReleasesViewController.h的代码(就像我在TVC中一样)

@interface UpcomingReleasesViewController : UICollectionViewController

@property (strong, nonatomic) NSMutableArray *upcomingReleases;

@end

我将此代码添加到我的UpcomingReleasesViewController.m(当我调用 cell.textLabel.text 时出错)

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *upcomingReleaseURL = [NSURL URLWithString:@"http://obscure-lake-7450.herokuapp.com/upcoming.json"];

    NSData *jsonData = [NSData dataWithContentsOfURL:upcomingReleaseURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.upcomingReleases = [NSMutableArray array];

    NSArray *upcomingReleasesArray = [dataDictionary objectForKey:@"upcoming_releases"];

    for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
        UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithName:[upcomingReleaseDictionary objectForKey:@"release_name"]];
        [self.upcomingReleases addObject:upcomingRelease];
    }
}

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

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

    UpcomingRelease *upcomingRelease = [self.upcomingReleases objectAtIndex:indexPath.row];

    cell.textLabel.text = upcomingRelease.release_name;

    return cell;
}

同样在我使用TVC时,我有一个名为“UpcomingRelease”的NSObject,代码如下:

UpcomingRelease.h

@interface UpcomingRelease : NSObject

@property (nonatomic, strong) NSString *release_name;

// Designated Initializer
- (id) initWithTitle:(NSString *)release_name;
+ (id) upcomingReleaseWithName:(NSString *)release_name;

@end

UpcomingRelease.m

@implementation UpcomingRelease

- (id) initWithTitle:(NSString *)release_name {
    self = [super init];

    if ( self ){
        self.release_name = release_name;
    }

    return self;
}

+ (id) upcomingReleaseWithName:(NSString *)release_name {
    return [[self alloc] initWithTitle:release_name];
}

@end

我应该为我的新应用创建一个NSObject并添加该代码,还是应该将其添加到我的UpcomingReleaseCell?

感谢。

2 个答案:

答案 0 :(得分:1)

您是否注册了自定义单元格?

    [self.collectionView registerClass:[UpcomingReleaseCell class] forCellWithReuseIdentifier:@"UpcomingReleaseCell"];

答案 1 :(得分:0)

  1. 使用情节提要时无需注册单元格

  2. 从故事板中选择CollectionViewCell,并通过选择

  3. 在身份检查器中将类更改为“UpcomingReleaseCell”
  4. 在collectionviewcell中添加标签,并将单元格连接到“UpcomingReleaseCell”,名称为“release_name”

  5. 在“cellForItemAtIndexPath”

    中添加以下内容
    (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UpcomingReleaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    // Configure the cell
    
    UpcomingRelease *upcomingrelease = [self.upcomingReleases objectAtIndex:indexPath.row];
    
    cell.release_name.text = upcomingrelease.release_name;
    
    return cell;
    }