2017-07-18 60 views
0

我有一個單元格,其中頂部有兩個標籤,中間是一個視圖,底部是兩個標籤,它是一個圖形。我硬編碼的項目數量,因爲我只需要10-13值,顯然一切都很好,值正在更新和子視圖添加在cellForRowAt委託方法,但在滾動標籤的值顯示爲空白,和這種行爲是非常不正常的,因爲如果第二個和第三個索引變爲空白,那麼在進一步滾動時他們會回來,第五個第五個變爲空白,因爲沒有服務調用,所以我不調用重新加載數據。這是絕對讓我瘋狂的,我知道它與單元格被重用時的關係,但我不知道爲什麼或者是什麼,我做錯了,因爲最初的值看起來很好。集合視圖單元格行爲異常

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
TraingingGraphCollectionViewCell *cell = (TraingingGraphCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"TraingingGraphCollectionViewCell"] forIndexPath:indexPath]; 

if (indexPath.row == 0 || indexPath.row == 12) { 

    [cell initEmptyCell]; 
} else { 

    [cell initCellWithMaximumWeight:100 completedWeight:10*(int)indexPath.row maxRepititions:10 completedRepititions:(int)indexPath.row]; 
} 

return cell;  
    }  

以下是在委託方法中調用的方法。 `

-(void) initEmptyCell { 
// [self setNeedsDisplay]; 

// back ground colour 
self.backgroundColor = UIColorFromRGB(COLOUR_232323); 

//hiding the labels 
[self.completedWeightLabel setHidden:YES]; 
[self.completedRepititonsLabel setHidden:YES]; 
[self.dateLabel setHidden:YES]; 
[self.setLabel setHidden:YES]; 
} 

這低於

-(void) initCellWithMaximumWeight:(float)maxWeight 
       completedWeight:(float)completedWeight 
       maxRepititions:(int)maxRepititions 
     completedRepititions:(int)completedRepititions { 

//reloading the content 
//[self setNeedsDisplay]; 


// back ground colour 
self.backgroundColor = UIColorFromRGB(COLOUR_232323); 

// adding the weight bar 
float weightPercentage = completedWeight/maxWeight; 
if (weightPercentage > 1) { 
    weightPercentage = 1; 
} 
UIView *weightView = [[UIView alloc] initWithFrame:CGRectMake(20.0, self.graphView.frame.size.height - (self.graphView.frame.size.height * weightPercentage), 10.0, self.graphView.frame.size.height * weightPercentage)]; 
[weightView setBackgroundColor:UIColorFromRGB(COLOUR_E9280E)]; 
[self.graphView addSubview:weightView]; 

// adding the repititions bar 
float repsPercentage = (float)completedRepititions/(float)maxRepititions; 
if (repsPercentage > 1) { 
    repsPercentage = 1; 
} 
UIView *repsView = [[UIView alloc] initWithFrame:CGRectMake(35.0, self.graphView.frame.size.height - (self.graphView.frame.size.height * repsPercentage), 10.0, self.graphView.frame.size.height * repsPercentage)]; 
[repsView setBackgroundColor:UIColorFromRGB(COLOUR_97F619)]; 
[self.graphView addSubview:repsView]; 

//bottom view 
[self.dateLabel setAttributedText:[NSString stringWithFormat:@"%@", [Utilities convertDateToString:[NSDate date] usingFormat:dd__MM_DATE_FORMAT forCulture:@"nl_NL"]] withFont:FontUsingMacro(LATO_BOLD_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_FFFFFF)]; 
[self.setLabel setAttributedText:[NSString stringWithFormat:@"Set 01"] withFont:FontUsingMacro(LATO_BOLD_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_808080)]; 

//top view 
[self.completedWeightLabel setAttributedText:[NSString stringWithFormat:@"%.1f KG", completedWeight] withFont:FontUsingMacro(LATO_REGULAR_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_E9280E)]; 
[self.completedRepititonsLabel setAttributedText:[NSString stringWithFormat:@"%d Reps", completedRepititions] withFont:FontUsingMacro(LATO_REGULAR_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_97F619)]; 


} 
+0

它很難找出沒有代碼的問題。 –

+0

我已經添加了代碼,請看看它。 –

+0

在initEmptyCell中,你隱藏了一些東西。但在initCellWithMaximumWeight中,您沒有取消隱藏它。單元格被重用(回收),因此您需要爲cellForRow中的所有情況設置它們。 – GeneCode

回答

1

cellForItem將被調用,即使沒有當你的細胞出現在屏幕重載給出的第二種方法。所以說你的索引0的單元格的值爲A.然後你滾動,0的單元格離開屏幕。索引8處的單元格顯示爲值B.當您向後滾動查看索引0處的單元格時,表格視圖可能會重用索引8處的單元格。現在,如果您沒有將其設置回正確的位置,索引0處的單元格將顯示B值爲索引0處的數據。

if foo { 
    cell.textLabel.text = "isFoo" 
} 

然後你的其他條件不代碼:

當你有一個聲明cellForItem像這種情況經常發生。

所以一定要確保你這樣做:

if foo { 
    cell.textLabel.text = "isFoo" 
} else { 
    cell.textLabel.text = "isNotFoo" 
} 

UPDATE:

現在,我看到你的代碼,這就是你做了什麼。您將標籤隱藏在您的if語句中,並且永遠不會在您的else語句中取消它們。

+0

謝謝。你是對的,這是我相信的問題。我已經取消了他們,現在一切正常。 –

1

將以下代碼包含在您的initCellWithMaximumWeight方法中。

[self.completedWeightLabel setHidden:NO]; 
[self.completedRepititonsLabel setHidden:NO]; 
[self.dateLabel setHidden:NO]; 
[self.setLabel setHidden:NO]; 
+0

感謝@Prakash的迴應。非常感激。 –

相關問題