2014-04-12 47 views
1

我想調用selectRowAtIndexPath對UITableView是我調用它的UIViewController的子視圖。selectRowAtIndexPath從另一個UIVIewController不起作用

我已經設置好了,所以當你選擇一個單元格時,它會灰色,這很好,但是我加載了不同的數據集進出UITableView,並且當有選擇時,我發送選定的NSIndexPath返回到UIViewController。然後,當視圖下一次加載正確的NSIndexPath數據集時,我從我的UIViewController調用此方法。

if (codeIndexPath != nil) { 
      [filterViewController.tableView selectRowAtIndexPath:codeIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle]; 
} 

然後在類的UITableView我的cellForRowAtIndexPathdidSelectRowAtIndexPath方法這個樣子的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Configure the cell... 
    NSString *projectDescriptionString = [currentFilterMutableArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = projectDescriptionString; 
    if (indexPath == currentlySelectedIndex) { 
     cell.highlighted = YES; 
    } else if (indexPath == currentlySelectedIndex) { 
     cell.highlighted = NO; 
    } 
    return cell; 
} 

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

    cell.selectionStyle = UITableViewCellSelectionStyleGray; 

    // send this data back in the delegate so you can use it to show where the tick is again if you need too. 
    currentlySelectedIndex = indexPath; 

    [[self delegate] updateInstallTableWithFilter:[currentFilterMutableArray objectAtIndex:indexPath.row] FilterType:filterType InstallIndex:indexPath]; 

} 

在加載屏幕上的正確的單元格將突出第二再回去白色..

任何幫助,將不勝感激。

UPDATE

新的,如果語句內的的cellForRowAtIndexPath

if ([indexPath isEqual:currentlySelectedIndex]) { 
     cell.highlighted = YES; 
    } else if (![indexPath isEqual:currentlySelectedIndex]) { 
     cell.highlighted = NO; 
    } 

我仍然reciving同樣的錯誤。

+0

你不應該用「==」比較indexPaths,因爲他們的對象。您應該使用isEqual:來代替。 – rdelmar

+0

不僅如果和如果都是==大聲笑錯過了一個..我更新了..會在一秒內顯示新的代碼。 – HurkNburkS

回答

4

UITableViewController有一個名爲clearsSelectionOnViewWillAppear的屬性。 From the doc

當表視圖即將出現它的加載在第一時間,所述 表視圖控制器重新加載表視圖的數據。每次顯示錶視圖時,它也會清除 的選擇(帶或不帶動畫,具體取決於請求) 。 UITableViewController類在超類方法viewWillAppear:中實現了這一點。您 可以通過更改 clearsSelectionOnViewWillAppear屬性中的值來禁用此行爲。

因此,在該表視圖控制器子類,在viewDidLoad中......

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.clearsSelectionOnViewWillAppear = NO; 
} 
+0

哇..我一直在嘗試這麼多東西,我在這一切中都得不到什麼......哦,今天我學到了一些新東西,讓我希望我下次再記住它。大聲笑非常感謝你的幫助。 – HurkNburkS

+0

我一直在努力拼搏,謝謝! – Johannes

相關問題