2009-05-22 69 views
45

在我的UITableView中,有時單元格會在觸摸後保持選中狀態。 因爲它只是偶爾發生,所以我無法重現 問題。iPhone UITableView單元保持選中狀態

任何提示?也許這與不正確的釋放tableView有關?

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSUInteger row = [indexPath row]; 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

switch (row) { 
    case 0: 
     FruitViewController *fruitController = [FruitViewController alloc]; 
     [fruitController retain]; 
     [fruitController initWithNibName:@"FruitView" bundle:[NSBundle mainBundle]]; 
     [self.navigationController pushViewController:fruitController animated:YES]; 
     [fruitController release]; 
     break; 
    case 1: 
     CerealsViewController *cerealsController = [CerealsViewController alloc]; 
     [cerealsController retain]; 
     [cerealsController initWithNibName:@"CerealsView" bundle:[NSBundle mainBundle]]; 
     [self.navigationController pushViewController:cerealsController animated:YES]; 
     [cerealsController release]; 
     break; 
    default: 
     break; 
} 
    } 
+3

也許你重寫viewWillAppear:動畫沒有調用[super viewWillAppear:animated]? – Tieme 2012-03-13 10:21:08

回答

98

我不能告訴你爲什麼你遇到的問題,但這裏有一些建議修復它:

據蘋果HIG,選擇不應該消失,直到從視圖返回控制器剛剛推入堆棧。如果你的控制器只是一個UITableViewController,它應該在返回到視圖時自動取消選擇。如果不是,則添加

- (void) viewWillAppear:(BOOL)animated { 
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; 
    [super viewWillAppear:animated]; 
} 

視圖控制器中的某處。

如果有,點擊後,沒有選擇的時候去另一種觀點認爲,不,其實做任何事情任何行,他們不應該是可選擇的,這樣就可以覆蓋在

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 

並且在不應該選擇該行的情況下返回nil

11

爲了展開「Ed Marty」的回答,我選擇在「viewWillDisappear」方法中添加取消選擇,因爲我認爲它看起來更好。

除此之外,我正在使用UITableView與常規UIViewController(而不是UITableViewController),所以tableView變量不可用於我。爲了克服這一點,我在我的視圖控制器頭文件中添加了的tableView屬性(在我的XIB文件我實際連接的表視圖屬性)...

@property (nonatomic,retain) IBOutlet UITableView *tableView 

...並在視圖控制器實現有線向上的屬性和如下進行取消選定..

@synthesize tableView 

- (void) viewWillDisappear:(BOOL)animated { 
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated]; 
    [super viewWillDisappear:animated]; 
} 
+0

謝謝,IBOutlet信息幫助了我。 – 2011-11-17 21:34:34

+0

由於所選答案在導航控制器中不起作用,所以此解決方案效果最佳 – codingrhythm 2013-01-05 07:52:30

21

一個可能的原因是覆蓋-viewWillAppear:animated:而不延伸UITableViewController控制器調用[super viewWillAppear]。在您的-viewWillAppear方法開始處添加[super viewWillAppear:animated]可能會糾正問題。

+0

這就是我要的,謝謝! – 2013-12-31 04:45:18

8

如果您正在使用一個UITableViewController你可以使用:

self.clearsSelectionOnViewWillAppear = YES; 
4

就我而言,我取消細胞就在didSelectRowAtIndexPath方法的末尾:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Do smth here. segue ..etc 

    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO]; 
} 
0

使用此方法在你UITableViewCell

(void)setSelected:(BOOL)selected animated:(BOOL)animated { 

// Just comment This line of code 

// [super setSelected:selected animated:animated]; 

} 

甚至你可以在didSelectRowAtIndexPath方法中嘗試這段代碼d

- (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {

//做當行選擇一些東西

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

}

0

解決這個問題大約需要兩秒鐘,一旦你瞭解了表的管理方式。

只要單元格在屏幕上滾動,就會失去格式。該表通過關閉其他單元並在用戶回滾時重新創建它們來最大限度地減少其內存使用量。

甚至像重新加載表格數據這樣的事情,真的只適用於當時屏幕上的單元格。要指出的是,cellForRowAtIndexPath可能會創建同一個單元五次,而用戶無需點擊按鈕或離開視圖控制器!所以設置它來重新加載任何格式。像這樣添加到cellForRowAtIndexPath:

if arrayOf_SelectedCells.contains(indexPath) { 
     setup_Selected(cell) 
     //Where setup_Selected is a custom function to apply formatt 
    } else { 
     setup_Unselected(cell) 
    } 
相關問題