2011-04-12 100 views
1

首先,我刪除tableView中的一個單元格,然後我想在tableView中添加另一個新單元格,但新單元格不是新的,它只是我刪除的單元格。我知道什麼dequeueReusableCellWithIdentifier means.i認爲當刪除一個單元格時,dequeueReusableCellWithIdentifier中的緩存也被刪除。爲什麼不呢?在tableview中刪除單元格,單元格也可以重用,爲什麼?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"table reload"); 

    PDFModel* tempmodel=[PDFArray objectAtIndex:indexPath.row]; 
    NSString * CellIdentifier=tempmodel.PDFName; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    NSLog(@"cell %x",cell); 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     //custom the cell 
     cell.text=... 
    } 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle==UITableViewCellEditingStyleDelete) 
    { 
     [PDFArray removeObjectAtIndex:indexPath.row]; 
     NSMutableArray* array=[[NSMutableArray alloc] init]; 
     [array addObject:indexPath]; 
     [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationRight]; 

    } 
} 
+0

你不應該返回細胞形成,如果你在那裏,如果你有一個取出的細胞檢查的語句。只需設置所有單元格屬性即可。 – rckoenes 2011-04-12 11:22:27

+0

此方法表明您要重用單元格 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; – GameLoading 2011-04-26 08:31:47

回答

2

沒有理由刪除UITableViewCell,因爲它可以重複使用。該單元不會被顯示,直到它再次被cellForRowAtIndexPath處理,因此它不會顯示舊數據。儘管佈局單元 - 視圖和所有子視圖 - 可以在新數據替換舊數據後重新使用。

想象一下,就像每個單元格是白板(乾擦板)一樣,它會被裁減到一定的大小,並且所有的基本信息區域都會被刻蝕到表面上:一些文本的地方,一個數字的另一個區域,用於繪製圖片的特殊空間等。在tableView:cellForRowAtIndexPath:中,當您檢查cell == nil是否正在嘗試查明是否有任何已蝕刻的白板可用時,您可以使用乾擦除來擦除和寫入新信息標記。如果沒有,那麼它會削減並蝕刻一塊全新的板子來寫信息,但是如果有一塊可以被擦除,那麼它就會用橡皮擦擦掉舊信息並寫入新信息。正如你可以想象的那樣,這節省了很多時間。

當用戶滾動什麼實際發生的表是當一個細胞(白板)滾動出頂部不再可見,系統擦除數據,並將其移動到堆的底部;它將成爲下一個滾動查看視圖。因此,系統只需要創建儘可能多的白板,而且任何時候都可以看到白板 - 屏幕外的實際上並不存在。

當你刪除一個單元格時,你真正要做的就是告訴表格(a)停止顯示該單元格,(b)設置一個標誌,以便表格可以使用橡皮擦擦除舊數據,但仍然重用白板。

創建單元格在計算能力方面可能非常昂貴。如果每次在用戶滾動時UITableView都需要創建一個新單元格 - 比方說,如果它不得不在每次看到新的白板時將所有區域蝕刻到其中, - 滾動會非常生澀,看起來很糟糕。通過重新使用單元格,只需更換不斷變化的內容,表格就可以在用戶與之交互時平穩移動。

2

你的問題有點奇怪,但我想你想知道下面的代碼是做什麼的?

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

什麼碼呢,就是它嘗試檢索緩存的UITableView細胞。這是爲了減少iPhone上的內存使用量。

一旦刪除單元格,你也應該從數據源(通常是一個數組)刪除數據,例如:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
     if (editingStyle==UITableViewCellEditingStyleDelete) 
     { 
      // remove object from array ... 
      [dataArray removeObjectAtIndex:indexPath.row]; 
     } 
} 

(記住:你只能從一個NSMutableArray在運行時從刪除項目,不NSArray)

如果單元格重繪,您將首先檢查哪個單元格需要重繪,然後從數據源檢索適當的數據,這一切都由UITableViewDataSource委託方法完成。您只需確保從數組中檢索正確的內容並在重繪時更新單元格的內容,例如像這樣:

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

    static NSString *CellIdentifier = @"CellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     // no cached cell is found, create a new cell ... 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     // add some defaults for cells in the initializer, 
     // stuff that's done here should be important for 
     // all displayed cells, for example background image. 
    } 

    // update the cell's content, e.g.: - it will only display content that's currently in the array, deleted objects shouldn't exist in the array at this point 
    NSString *title = [dataArray objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:title]; 

    return cell; 
} 
+0

謝謝,我的英語太窮了〜起初,我刪除tableView中的一個單元格,然後我想在tableView中添加另一個新單元格,但新單元格不是新的,它只是我刪除的單元格。我知道dequeueReusableCellWithIdentifier意味着什麼。我想當你刪除一個單元時,dequeueReusableCellWithIdentifier中的緩存也會被刪除。爲什麼不呢? – 2011-04-12 15:50:48

+0

刪除單元格後,應該刷新tableview(在tableView實例上使用** reloadData **)。這樣,所有的內容都會重新繪製,並且刪除的單元格應該已經消失,包括它的緩存內容。 – 2011-04-12 16:08:31

+0

謝謝!我會嘗試一下tomorow〜 – 2011-04-12 16:14:14

2

我認爲你可以使用

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];

,而不是

NSString * CellIdentifier=tempmodel.PDFName;

請更換&檢查。我認爲這對你有幫助。

0

您似乎對刪除單元格(一個Objective-c UI對象)和它顯示的數據之間的區別感到困惑。

  • 當你允許用戶刪除單元格在用戶界面中,你需要從你的數據模型

  • 的UITableView的實現iOS中是高度優化和它緩存單元(刪除相應的數據UI對象)來提高性能。您並不需要擔心這一點,因爲您的表格將根據您的數據進行最佳加載和顯示。

tableView:cellForRowAtIndexPath:你做到以下幾點:

  1. 試圖出列的單元實例。這利用了UITableView完成的緩存優勢

  2. 如果出隊未返回單元格,請創建一個新單元格。

  3. 一旦你有一個細胞,從你的數據模型

  4. 相應數據配置它當用戶選擇從小區的刪除操作,從數據模型中刪除的數據。可選地,您可以動畫從表格中刪除單元格或者簡單地重新加載整個表格。

#4的細節取決於您想要的用戶界面以及您如何配置數據模型。

蘋果文檔對解釋這個很好。

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html