2013-11-28 15 views
0

我在xib文件中有兩個tableviewcell,我使用它們兩個。內存warrning tableviewcell

當我使用其中一個內存仍然是7.3MB,但是當我使用這兩個內存增長非常快。

請幫幫我。我現在不是什麼錯。

我的代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSUInteger row = [indexPath row]; 
    NSDictionary *rowData = [self.events objectAtIndex:row]; 

    static NSString *CellIdentifierL = @"LeftCell"; 
    static NSString *CellIdentifierR = @"RightCell"; 


    SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL]; 
    SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR]; 

    if (cellLeft == nil) { 
     NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LeftTableCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[SampleTableCell class]]) { 
       cellLeft = (SampleTableCell *)currentObject; 
       break; 
      } 
     } 
    } 

    if (cellRight == nil) { 
     NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[SampleTableCell class]]) { 
       cellRight = (SampleTableCell *)currentObject; 
       break; 
      } 
     } 
    } 



    return (row%2==0)?cellRight:cellLeft; 
} 
+0

你有沒有檢查你的內存崩潰報告或intrument日誌,因爲你的代碼看起來很好,你在做什麼與細胞有一些你已離開我猜 – Retro

+0

我看到這段代碼沒有問題。內存佔用量何時增長?滾動表格視圖時? –

+0

兩個理論:1-你確定你的'SampleTableCell'有正確的reusableCellIdentifier嗎? (檢查你的xib)如果沒有,這意味着你正在創造太多的行。 2-你應該確定在創建它們之前你必須創建哪個單元格。儘管如此,這隻對第一個細胞的創造很重要。 –

回答

0

這不是做到這一點的好辦法。您正在爲每一行創建兩個單元格,但只能使用其中的一個單元格。您還應該使用更新的方式來使用基於xib的單元格 - 即使用registerNib:forCellWithReuseIdentifier:在viewDidLoad中註冊兩個nib。首先檢查奇數/偶數行,並將正確的單元格出列(使用註冊方法時不需要檢查零)。你這樣做:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"SampleCell1" bundle:nil] forCellReuseIdentifier:@"SampleCell1"]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"SampleCell2" bundle:nil] forCellReuseIdentifier:@"SampleCell2"]; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.row % 2 == 0) { 
     SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SampleCell1" forIndexPath:indexPath]; 
     // populate cell with data 
     return cell; 
    }else{ 
     SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SampleCell2" forIndexPath:indexPath]; 
     // populate cell with data 
     return cell; 
    } 
} 

這就是你需要做的。不需要檢查零單元格,因爲dequeueReusableCellWithIdentifier:forIndexPath:如果您註冊了一個筆尖,或者在故事板中創建單元格(在這種情況下您不需要註冊任何內容),則可以保證返回有效的單元格。

+0

你有什麼例子嗎? 我應該在viewDidLoad中創建我的控制器嗎? 以及我如何在cellForRowAtIndex中使用它 – Unmerciful

+0

創建兩個單元應該不成問題。方法返回時,ARC應立即釋放未使用的單元格。雖然在這種情況下將自己設置爲筆尖的擁有者是否會抵消ARC,這是一個有趣的問題。 你可以在不使用owner的情況下嘗試它:loadNibNamed中的self:? –

+0

@Unmerciful,我已經更新了我的答案,告訴你如何做到這一點。我爲這個例子創建了一個類SampleCell和兩個xib文件SampleCell1和SampleCell2。 – rdelmar

0

您只需要創建所需的單元格。您的代碼應該是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSUInteger row = [indexPath row]; 
    NSDictionary *rowData = [self.events objectAtIndex:row]; 

    static NSString *CellIdentifierL = @"LeftCell"; 
    static NSString *CellIdentifierR = @"RightCell"; 

    if (cellLeft == nil) { 
     SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL]; 
     NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LeftTableCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[SampleTableCell class]]) { 
       cellLeft = (SampleTableCell *)currentObject; 
       break; 
      } 
     } 
     return cellLeft; 
    } 

    if (cellRight == nil) { 
     SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR]; 
     NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[SampleTableCell class]]) { 
       cellRight = (SampleTableCell *)currentObject; 
       break; 
      } 
     } 
     return cellRight; 
    } 



    return nil; 
} 
0

我改成這樣:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSUInteger row = [indexPath row]; 
    NSDictionary *rowData = [self.events objectAtIndex:row]; 

    SampleTableCell *cell; 


    if(row%2==0){ 

     cell = [[[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil]objectAtIndex:0]; 

    }else{ 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"LeftTableCell" owner:self options:nil]objectAtIndex:0]; 
    } 

    cell.labelName.text = [rowData objectForKey:@"name"]; 

    return cell; 
} 

但我不知道這是好主意嗎?