2013-10-08 44 views
1

我正在製作具有自定義單元格的UICollectionView,並且發生了一件非常奇怪的事情。所有indexPath.row的數字爲ODD的單元格留空,我無法對它們執行任何繪製。UICollectionView奇數單元格留空

我使用故事板在我自己的UIViewController中創建了一個UICollectionView對象。 UICollectionView的單元格被設置爲我自定義的UICollectionViewCell sublcass,名爲CustomCell。每個單元佔據整個寬度和高度UICollectionView。 CustomCell內的所有內容都是以編程方式創建的,而不是使用Storyboard。這裏是我的cellForItemAtIndexPath代碼:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    //createViewWithDay is used to populate the contents of the cell 
    [cell createViewWithDay:indexPath.row isToday:YES withSize:cell.frame.size]; 

    NSInteger x = indexPath.row; 
    NSLog(@"Path: %i", x); 

    return cell; 
} 

每個CustomCell創建一個自定義視圖(命名爲CustomView),並將它作爲一個子視圖。現在,所有CustomView都會繪製一個X軸和一個Y軸。

奇怪的是,cellForItemAtIndexPath正確觸發每個單元格。也就是說,它被稱爲偶數和奇數指數。與委託方法didSelectItemAtIndexPath相同。每個CustomView的圖形不會根據單元格的索引進行更改。實際上,根據指數,沒有任何變化。這裏是我運行應用程序時出現的一個例子。

在第一張圖片中,繪製座標軸的單元格爲indexPath.row == 14,而黑色單元格爲indexPath.row == 15。在第二張圖片中,索引15位於左側,索引16位於右側。 enter image description here

有沒有人知道爲什麼會發生這種情況?奇數/偶數索引可能不相關,但這就是發生了什麼。

編輯: 一些額外的代碼.. 這裏是createViewWithDay,這就是所謂的cellForItemAtIndex方法:

- (void)createViewWithDay:(float)day isToday:(BOOL)today withSize:(CGSize)size 
{ 

    CustomView *newView = [[CustomView alloc] initWithFrame:self.frame]; 

    [newView viewForDay:day overDays:6 withDetail:20 today:YES withSize:size]; 

    [newView setBackgroundColor:[UIColor whiteColor]]; 
    [self addSubview:newView]; 
} 

這裏是viewForDay

- (void)viewForDay:(NSInteger)primaryDay overDays:(NSInteger)days withDetail:(NSInteger)detail today:(BOOL)today withSize:(CGSize)size 
{ 
    _graphPrimaryDay = primaryDay; 
    _numberOfDays = days; 
    _lineDetail = detail; 
    _isToday = today; 
    _viewSize = self.frame.size; 
    _pointsPerDay = (float)(_lineDetail/_numberOfDays); 
    _isReadyToDraw = YES; 

    [self createGraphDays]; 
} 

viewForDay方法簡單地分配一些CustomView實例變量,而createGraphDays方法popu用啞元數據描述一個NSMutableArray。

我想我還要補充CustomViewdrawRect方法,所以這裏是..

- (void)drawRect:(CGRect)rect 
{ 
    if(_isReadyToDraw) 
    { 
     [self drawGraph]; 
    } 
} 

這裏是drawGraph ..

- (void)drawGraph 
{ 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextMoveToPoint(context, 0, _viewSize.height/2); 
    CGContextAddLineToPoint(context, _viewSize.width, _viewSize.height/2); 

    if(_isToday) 
    { 
     CGContextMoveToPoint(context, _viewSize.width/2, 0); 
     CGContextAddLineToPoint(context, _viewSize.width/2, _viewSize.height); 
    } 

    CGContextSetLineWidth(context, 2.5); 
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); 

    CGContextStrokePath(context); 
} 

謝謝!

+1

你可以發佈代碼爲'[cell createViewWithDay:indexPath.row isToday:YES withSize:cell.frame.size];' – Alex

回答

2

我相信我已經找到了解決我的問題的方法。在前兩個單元格(索引0和1)之後,我的自定義視圖的drawRect方法不再被調用。無論出於何種原因,自定義視圖都被自定義單元格的背景顏色覆蓋,該背景顏色爲黑色。所以我通過在我的自定義視圖上調用setNeedsDisplay來修復它,以便強制爲每個單元繪製正確的圖像。

編輯:

這是這對這個問題的情況下正確的答案,但我實際上已經遇到了同樣的問題出於不同的原因。

對於舊的解決方案,我所做的只是確保[cell createViewWithDay:isToday:withSize:](從cellForRowAtIndexPath調用)包含[self.view setNeedsDisplay],以便手動更新單元的視圖。

但是,正如我所說,我遇到了同樣的問題,其他所有單元都留空,但出於不同的原因。在cellForRowAtIndexPath中,我正在調用該單元的設置方法。這個方法反過來會調用子視圖的設置方法。子視圖的設置方法的一個參數是單元格的框架,所以它會沿着(在collectionViewCellSubclass[self.view setupViewWithFrame:(CGRect)frame]的行。

對於第一單元,此工作得很好,但對於第二小區時,正在傳遞的幀向視圖是(320, 0, 320, 250)。由於單元格相對於集合視圖的起點爲(320, 0),因此單元格的子視圖始終爲(320, 0),但相對於單元格,因此它實際上將放置在屏幕外。這只是通過初始化子視圖的大小,並確保其來源是(0, 0)。這整個問題現在變得非常長,但希望這個替代問題和解決方案將會有所幫助。

+0

請在這裏添加一些代碼。這可能有助於某人。 –

相關問題