gpt4 book ai didi

ios - 将 TableView Controller 更改为 Collection View Controller

转载 作者:行者123 更新时间:2023-11-29 03:39:43 25 4
gpt4 key购买 nike

我有一个 TableView Controller ,我想将它更改为一个 Collection View Controller ,我的应用程序使用 JSON 获取它的信息。

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

我想转移我在 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 中?

谢谢。

最佳答案

您注册了自定义单元格吗?

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

关于ios - 将 TableView Controller 更改为 Collection View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18620786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com