0

私は現在UICollectionViewController、サーバーから画像をロードし、画像UICollectionViewCellsUICollectionView. 以前よりも簡単にするために、データベースからプルするのではなく、ローカルに保存されている画像の配列を作成するだけです。動作しますが、何らかの理由で、スクロールを開始するまですべてのセルに画像が表示されるわけではありません。

何が起こっているかの画像はこちらです!

画像

私はいくつかのチュートリアルを読みましたが、それらはすべて使用していますstoryboard...これをプログラムで書きたいと思っています。

いくつかのコードを次に示します。

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    JSMediaCollectionController *instagram = [[JSMediaCollectionController alloc] initWithCollectionViewLayout:[UICollectionViewFlowLayout new]];
    instagram.datasource = self;
    self.array = [NSMutableArray array];
    for (int i = 0; i < 100; i++) {
        [self.array addObject:@"instant_noodle_with_egg.jpg"];
    }

    UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:instagram];
    self.window.rootViewController = navCon;
    [self.window makeKeyAndVisible];
    return YES;
}

- (NSArray *)itemsToLoad
{
    return [NSArray arrayWithArray:self.array];
}

JSMediaController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[JSMediaCollectionCell class] forCellWithReuseIdentifier:reuseIdentifier];


    self.showOneItemPerRow = false;
    // Do any additional setup after loading the view.
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[self.datasource itemsToLoad] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMediaCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell setImage:[UIImage imageNamed:[[self.datasource itemsToLoad] objectAtIndex:indexPath.row]]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
    return cell;
}

...

...

JSMediaCell.m:

@interface JSMediaCollectionCell()
@property (nonatomic) UIImageView *view;
@end

@implementation JSMediaCollectionCell

- (id)initWithFrame:(CGRect)frame
{
    if (!(self = [super initWithFrame:frame])) return nil;
    self.view = [[UIImageView alloc] initWithFrame:self.frame];
    [self.viewForBaselineLayout addSubview:self.view];
    return self;
}

- (void)setImage:(UIImage *)image
{
    self.view.image = image;
}

誰かが私を正しい軌道に乗せるために助けてくれたことに非常に感謝しています...念のため...スクロールを開始すると、画像がリロードされますが、すべてのセルに画像が読み込まれているわけではありませんsubview...ほんの一部です。

4

1 に答える 1

0

viewでセルファイルのプロパティを初期化していることがわかりますself.frame

代わりに、 self.view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

何がうまくいかなかったのか推測できると思います!!!

フレームはセルに書き込みます。サブビューがセルに追加されると、セルの値を取得するoriginため、セルのビューから外れます

于 2014-12-04T05:51:46.027 に答える