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的值,因此它會從單元格的視圖中移出

相關問題