2010-10-20 76 views
32

我在這裏跟蹤了Luke Redpath的建議 - Selected UItableViewCell staying blue when selected - 在返回原始表格視圖時取消選擇行,但我無法使其工作。實際上該方法不會被觸發。返回時取消選擇表格視圖行

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; 
} 

我懷疑這是因爲該類不是一個UITableViewController,但與tableView爲連接到NIB一個屬性UIViewController

我將如何獲得相同的行爲 - 即取消選擇何時返回?

+0

只要tableView連接正確,它應該沒有關係。你應該可以把它放在'didSelectRowAtIndexPath:'中,並讓它工作。你也可能想做一個快速的NSLog檢查'[self.tableView indexPathForSelectedRow]'返回的結果,因爲它實際上可能返回nil。 – iwasrobbed 2010-10-20 19:24:25

+0

我用這個,但有一個問題,改變單元格中的東西(背景等)沒有立即反映出來。添加reloadRowsAtIndexPaths:在didDeselectRowAtIndexPath中:做了竅門 – nh32rg 2013-04-15 18:38:28

回答

15

如果您使用的是NavigationController,你可以用它的委託做到這一點:

- (void)navigationController: (UINavigationController *)navigationController 
     didShowViewController: (UIViewController *)viewController 
        animated: (BOOL)animated 
{ 
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; 
} 

但你必須檢查顯示控制器是否是你想要=/

[編輯] 創建該出口並將其鏈接到IB到NavigationController

@interface MyInt <UINavigationControllerDelegate> 
{ 
    IBOutlet UINavigationController *navigation; 
    ... 
} 
@property (nonatomic, retain) UINavigationController *navigation; 
... 
@end 

@implementation MyInt 
@syntesize navigation; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.navigation setDelegate: self]; 
} 
... 

@end 
+0

我的導航控制器是在我的筆尖中創建的,因此它沒有相應的類。有沒有辦法解決這個問題? – cannyboy 2010-10-20 12:17:50

+0

您可以將其映射到IBOutlet伊娃。 – 2010-10-20 12:22:27

+0

我的設置是我有一個MainViewController類和一個相應的NIB。該筆尖具有UITabBarController(也是MainViewController中的IBOutlet屬性「tabBarController」)。在該標籤欄中有幾個UINavigation控制器。這些未在代碼中引用。所以我不確定如何訪問任何這些導航控制器 – cannyboy 2010-10-20 12:46:54

8

您是否以編程方式選擇行?如果是這樣,當使用[cellToBeSelected setSelected:YES];選擇一行時,我遇到了同樣的問題。

通過使用UITableViewControllerselectRowAtIndexPath:animated:scrollPosition:方法,它可以跟蹤當前選擇的indexPath。

實際上,如果clearsSelectionOnViewWillAppear設置爲true,則不會手動取消選擇行。

21

爲什麼你剛纔不委託方法

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

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; 

} 
+2

您不必執行'[self.tableView indexPathForSelectedRow]',indexPath已經是一個參數,您可以在其中插入它。 – 2014-04-14 00:24:25

+0

另外,如果您使用委託方法,則應該使用'tableView'參數而不是'self.tableView',因爲如果您有多個tableview,它們可能會有所不同。 – 2015-11-25 20:00:57

8

我哈得同樣的問題取消了,但調試後,我發現了indexPathForSelectedRow計數爲0,所以我仔細檢查了我的代碼,發現我呼叫

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

    // some code... 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

刪除此行後,它只是按預期工作。

+0

這是一個完全明顯的錯誤,抓住了我。我認爲包括我自己在內的很多人都把這個取消選舉放在了代表出於習慣。 – micnguyen 2016-01-04 13:06:44

4

很簡單:試試這一行。

添加deselectRowAtIndexPathdidSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
0
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor grayColor]]; 
} 

-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor clearColor]]; 
} 
+0

請描述爲什麼這是解決方案。什麼是變化/技巧? – 2016-02-18 10:32:37

1

雨燕3.0

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
      tableView.deselectRow(at: indexPath, animated: true) 
    //Write Your Other code Here. 
      } 

這將取消選擇行你點擊它之後。

相關問題