2010-11-06 55 views
0

我創建了一個自定義的UITableView單元,它有4-5個不同的組件。有時我會根據CGRectMake創建UIViews,並根據一些x,y座標計算來定位一些UIImages。 Everthing工作正常cos cellForRowAtIndexPath每次創建一個新的單元格。但是當我要重用基於「dequeueReusableCellWithIdentifier」的單元時,UI會變得混亂。之前的矩形使得從未被擦除,早期的UIViews的定位也表現怪異。雖然單元格索引是變化,但它並不反映繪製的UIImages的協調。每次關閉UITableViewCell cellForRowAtIndexPath調用

在每個單元格中創建UITableViewCellForRowAtIndexPath在3GS和4G中工作正常,但它真的非常煩人,而且它在3G中非常慢。我懷疑這種行爲是因爲在每次調用中創建了這個cellForRowAtIndexPath。所以任何人都可以幫助我找出這個問題。

這就是我現在要做的。

CustomNoteCell *cell = nil; 

if (cell == nil) { 

    cell = (CustomNoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomNoteCell" owner:nil options:nil]; 


for (id currentObject in topLevelObjects) { 

    if ([currentObject isKindOfClass:[UITableViewCell class]]) { 

      cell = (CustomNoteCell *) currentObject; 
      break; 
    } 
} 
} 

//rest of the codes go here like 
CGSize maximumLablelSize = CGSizeMake(296, 1000); 
CGSize expectedLabelSize = [alue sizeWithFont:cell.customCellNoteText.font constrainedToSize:maximumLablelSize lineBreakMode:cell.customCellNoteText.lineBreakMode]; 
CGRect newFrame = cell.customCellNoteText.frame; 
newFrame.size.height = expectedLabelSize.height; 
cell.customCellNoteText.frame = newFrame; 
cell.customCellNoteText.text = noteValue; 
cell.uiViewContrlloer = self; 
CGRect addressRect = cell.address.frame; 
addressRect.origin.y = newFrame.origin.y + newFrame.size.height + 10; 
cell.address.frame = addressRect; 

謝謝

回答

1

我覺得你把你的離隊細胞的呼叫在錯誤的地方。你應該把它放在if (cell == nil)之前。

+0

是。我已經發布了工作示例。在這種情況下,它工作正常。但在3G中有緩慢的表現,但在3GS和4G上表現完美。但是,當我在if(cell == nil)之前將單元出隊時,我得到了我討論的問題。所以這是我的問題。我想在該線以上出院,它應該按預期工作,但目前尚不能。 – Dilshan 2010-11-06 07:11:47

+0

你可以發佈你的實際代碼,將無法正常工作,並與格式?我的眼睛現在受傷了... – tia 2010-11-06 07:22:34

+0

對不起。格式化它。 – Dilshan 2010-11-06 14:23:24

0

正如tia提到的那樣,你的細胞總是會變成零。你應該儘早出隊。

CustomNoteCell *cell = (CustomNoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (! cell) { 
    cell = [[[CustomNoteCell alloc] initWithReuseIdentifier:CellIdentifier] autorelease]; 

...

+0

謝謝Joshpaul。當我要重用基於「dequeueReusableCellWithIdentifier」的單元格時,UI會變得混亂。這是問題。 – Dilshan 2010-11-08 05:02:14

+0

什麼是「搞砸了?」這種方法會發生什麼? static NSString * CellIdentifier = @「CustomNoteCell」; CustomNoteCell * cell =(CustomNoteCell *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(!cell) cell = [[[NSBundle mainBundle] loadNibNamed:@「CustomNoteCell」owner:self options:nil] lastObject]; } //定製單元格... – joshpaul 2010-11-10 16:46:10

+0

這裏的問題是定位出錯。它在每個單元格中隨機抽取內容。 – Dilshan 2010-11-16 10:12:27

相關問題