2012-12-06 31 views
0

我創建了一個自定義的UITableViewCell三的UILabel和一個的UIImageView, 1:我創建了子類的UITableViewCell的, 2:添加了三個標籤,一個的UIImageView,也是在廈門國際銀行文件 3:用以下方式的cellForRowAtIndexPath怎麼也得從筆尖自定義的UITableViewCell可重複使用

static NSString *CellIdentifier = @"NarrativeCell"; 

NarrativeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil){ 
    NSLog(@"New Cell Made"); 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NarrativeCell" owner:nil options:nil]; 

    for(id currentObject in topLevelObjects) 
    { 
     if([currentObject isKindOfClass:[NarrativeCell class]]) 
     { 
      cell = (NarrativeCell *)currentObject; 
      break; 
     } 
    } 
} 

Narrative n=(Narrative*)[tableArray objectAtIndexPath:indexpath.row]; 

[cell.articleName setText:n.title]; 

NSData *data=[[NSData alloc]initWithContentsOfFile:n.file] 
[cell.sideImageView setImage:[UIImage imageWithData:data]]; 

問題創建細胞是這樣的,它總是會創建新的細胞和我添加一些圖片後,讓內存警告和崩潰。 請幫助如何可以重用上面代碼的單元格,以便它不應該使用多少內存

+0

欺騙×2 http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files http://stackoverflow.com/questions/413993/loading-a -reusable-的UITableViewCell-從-A-筆尖 –

回答

0

你的代碼被罰款。你永遠不設置單元格以便它們不會重複使用的可重複使用的標識(如你注意到)。

在你定製細胞類中,實現該方法reuseIdentifier:

- (NSString *) reuseIdentifier { 
    return @"NarrativeCell"; 
} 

或設置在界面生成器的識別符(在檢查員的小區)。

相關問題