0

我目前正致力于编写一个UICollectionViewController以从服务器加载图像,并将图像置于UICollectionView内的自定义UICollectionViewCells中。为了让事情变得更简单比它已经是,我不拉从一个数据库中,我只是简单地创建存储在本地的图像阵列。它起作用,但由于某种原因,直到我开始滚动时,并非所有单元格都显示图像。开始使用UICollectionViewController以编程方式

继承人怎么回事的形象!

Image

我已经阅读了关于几个教程,但它们都使用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加载图像...只是一些。

+1

您不应该在setImage中添加子视图。当单元格被重用时,您将为已经具有图像视图的单元添加图像视图。最好在init方法中设置子视图。 – rdelmar 2014-12-04 05:36:28

+0

酷酷...感谢您的建议。我做了 – jsetting32 2014-12-04 05:39:07

+0

你应该把你的观点财产.h文件更新,所以你可以cell.view访问它的cellForRowAtIndexPath。没有理由重写setImage。 – rdelmar 2014-12-04 05:40:52

回答

0

我可以看到你与self.frame初始化在细胞文件view属性。

相反,它应该是 self.view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

我想你可以猜到了什么问题。!!!

帧应该是WRT到电池中。 当子视图添加到单元格时,它将使用单元格的origin的值,因此它会从单元格的视图中移出