2011-06-16 23 views
0

我正在加載一個UITableView有時它會有5個單元格,有時4個單元格。根據有多少個單元格,我想爲第2行或第3行設置AccessoryDe​​tail按鈕。我知道這個條件起作用,因爲我已經用didSelectRowAtIndexPath:成功地嘗試了它,但由於某種原因,TableView似乎沒有得到根據顯示的行數來更新。我正在使用[tableView reloadData]成功地在viewWillAppear:中重新加載TableView數據,但是這對我來說並沒有照顧到AccessoryDe​​tail問題。我試過使用[tableView reloadInputViews]無濟於事。問題是AccessoryDe​​tail圖像總是設置爲第2行或第3行,這取決於我從應用程序開始加載哪個視圖。iOS的AccessoryDe​​tail圖像不重新更新時UITableView

下面是來自cellForRowAtIndexPath:方法的邏輯:

if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) { 
      cell.selectionStyle = UITableViewCellSelectionStyleGray; 
      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
     } 

編輯: 我已經改變了西門依李的帶有else子句的建議,我的方法是這樣的,但它似乎沒有任何工作:

if (cell == nil) { 

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OfficeCellIdentifier] autorelease]; 


if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) { 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
//NSLog(@"row == 2 && [[self.office boxAddress] length] == 0 || row == 3"); 
} else { 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

} 
+0

@請詳細說明您的問題。通過發佈您的cellForRowAtIndexPath方法或發佈屏幕截圖。 – 2011-06-16 12:45:43

回答

1

將if-else語句放在if(cell == nil)代碼塊之外。如果你重新使用這個單元格,你的代碼都不會被調用。

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OfficeCellIdentifier] autorelease]; 
} 

if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) { 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
} else { 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
+0

謝謝,最後我經歷了一些反覆試驗後發現我自己。 – 2011-06-16 14:06:32

1

您應該重新設定選擇款式和配件的類型,你有沒有else子句在那裏......一旦你設置它,就是這樣,如果你重用的小區,他們將永遠不會得到他們的附件復位... 。

+0

我添加了else子句,但它似乎沒有做任何事情(請參閱我的編輯)。 – 2011-06-16 13:28:17