2013-02-22 62 views
3

我在UINavigationController中使用了具有UITableView的storyboard。 在這個UITableView中,使用了具有內部屬性的自定義tableViewCell。segue不能與UITableViewCell alloc一起工作,但dequeueReusableCellWithIdentifier

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

    CustomTableViewCell *cell = nil; 

    if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { 

     //iOS 6.0 below 
     cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    } 
    else { 
     //iOS 6.0 above 

     cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; //work segue 

    } 

上面的代碼適用於push segue。但不是當我用

 cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; //not work segue 

我用這種alloc方法保留單元格的數據重用單元格。

這只是分配vs deque ..方法的區別。我錯過了什麼?

編輯)我知道不使用dequeReusableCell方法對性能原因不利。但是,細胞的數量不會很多。這就是爲什麼我不需要deque方法。

  1. 「不工作」的意思是「不要執行推繼續」,而不是崩潰。

    它顯示的單元格與使用dequeReusable方法時相同,但單元格右側的揭示指示器圖標除外。指示器圖標來自故事板設置。

    當我觸摸單元格時,單元格突出顯示爲藍色,但不推動push segue。

  2. CustomTableViewCell有4個屬性。這與UITableViewCell完全不同。用戶在DetailViewController中設置屬性(推動segue引領這一點)。該單元格沒有IBOutlet參考。在MasterViewController(具有tableView)中,cellForRowAtIndexPath方法返回上面的CustomTableViewCell代碼。

  3. cellForRowAtIndexPath方法在CustomTableViewCell上的指標左邊添加一個開/關按鈕 併爲該單元設置一個標籤號。

+0

請注意,當使用Storyboard時,'dequeueReusableCellWithIdentifier'總會給你一個單元格。 – Ric 2013-02-22 23:35:55

回答

8

使用dequeueReusableCellWithIdentifier是什麼使您能夠使用您的原型單元格。如果您使用initWithStyle而不是dequeueReusableCellWithIdentifier,那麼您不需要,因此您也不會丟失任何細分,披露指標以及您爲這些細胞原型定義的其他UI外觀。

如果你決定走這條路線,你必須去「老派」(即做我們以前在細胞原型之前做過的事情)並寫下你自己的didSelectRowForIndexPath。但是,如果你已經有SEGUE定義,讓我們說你把它叫做「SelectRow」,那麼你的didSelectRowForIndexPath可以執行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    [self performSegueWithIdentifier:@"SelectRow" sender:cell]; 
} 

如果您需要在您的披露指標,那麼你的自定義單元格例程(或cellForRowAtIndexPath )將不得不手動設置。如果你有

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

添加它,那麼你需要手動處理:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    [self performSegueWithIdentifier:@"SelectAccessory" sender:cell]; 
} 

底線是,你可以得到這個工作,但你只是做了很多額外的工作和失去了丟失細胞的性能和記憶效益。我衷心鼓勵您重新考慮不使用dequeueCellWithIdentifier的決定。

+0

謝謝。清晰和乾淨的答案。並感謝您的建議。 – bureaucoconut 2013-02-23 08:18:32

相關問題