2013-05-08 36 views
0

在我發佈問題本身之前,我需要說明這是一個越獄應用程序。這就是爲什麼我正在寫文件系統中的「奇怪」文件夾。不能從didSelectRowAtIndexPath更改附件類型?

讓我們繼續。

這裏是我的cellForRowAtIndexPath方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"pluginCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 
    if(indexPath.row == 0) 
    { 
     cell.textLabel.text = @"default"; 
    }else 
    { 
     //Get the plugin's display name. 
     NSBundle *currentPlugin = [[NSBundle alloc] initWithPath:[NSString stringWithFormat:@"/Library/Cydeswitch/plugins/%@", [plugins objectAtIndex:indexPath.row - 1], nil]]; 
     cell.textLabel.text = [[currentPlugin localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"]; 
     if(cell.textLabel.text == nil) 
     { 
      //No localized bundle, so let's get the global display name... 
      cell.textLabel.text = [[currentPlugin infoDictionary] objectForKey:@"CFBundleDisplayName"]; 
     } 
     [currentPlugin release]; 
    } 

    if([[[cell textLabel] text] isEqualToString:[settings objectForKey:@"pluginToExecute"]]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     currentCell = [cell retain]; 
    } 
    return cell; 
} 

就像你所看到的,這種方法使用了一個名爲currentCell成員,使其指向當前「選擇」單元格。這是一個選項表,用戶應該只能隨時在Checkmark附件圖標上顯示一個單元格。

當使用情況選擇另一個單元格時,他正在更改選項,並且複選標記應該從當前單元格中消失並出現在新出現的單元格中。我這樣做:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    currentCell.accessoryType = UITableViewCellAccessoryNone; 
    [currentCell release]; 
    currentCell = [[self tableView:tableView cellForRowAtIndexPath:indexPath] retain]; 
    NSLog(@"CURRENT CELL %@", currentCell.textLabel.text); 
    currentCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

但它不起作用。當我點擊另一個單元格時,Checkmark正確地從舊單元格消失,但它從不出現在新單元格中。

我知道選擇工作正常,因爲NSLog在那裏打印新單元格的文本就好了。

我已經嘗試過跟蹤indexPath,但它根本不起作用。當我嘗試使用indexPaths而不是指向單元格的指針時,當用戶點擊單元格時根本沒有發生任何事情(至少在我目前的方法中,複選標記從舊單元格中消失)。

我認爲它與cellForRowAtIndexPath有關,因爲如果我一直指向單元格,複選標記會消失,但由於某種原因,當嘗試更改從cellForRowAtIndexPath獲取的單元格中的附件類型時,它似乎不起作用所有。

任何幫助將不勝感激。

回答

2

錯字?試試這個:

currentCell = [[self.tableView cellForRowAtIndexPath:indexPath] retain]; 
+0

是的,這個解決方案與上面的答案一起工作,哈哈。混合的答案。 – 2013-05-08 15:10:50

1

你不能保留最後選定單元格的跟蹤你的方式。細胞得到重用。使用伊娃來跟蹤indexPath或其他適合您數據的密鑰。

然後在didSelect...方法中,您使用保存的indexPath或鍵獲取對舊單元格的引用。在cellForRow...方法中,您需要根據當前的indexPath是否與保存的indexPath匹配來設置正確的accessoryType。

最後,不要調用你自己的委託/數據源方法。獲取單元格的引用時,請直接詢問表格視圖。

順便說一句 - 你在cellForRow...方法中過度保留currentCell。除非是第一次做任務,否則沒有必要全部保留在該方法中。

+0

謝謝。我認爲解決方案與上面的答案一樣。因爲我確實在追蹤最後的索引路徑,但結果是一樣的。我會嘗試。謝謝。 – 2013-05-08 15:06:58