2012-10-18 68 views
0

我得到了一個UITableViewCell,我想保持選中時單擊它。所以我有這樣的代碼非常有效:UITableViewCell顯示切換時選擇視圖

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [[[self.dataSource objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension]]; 
    //cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    UIView *bgColorView = [[UIView alloc] init]; 
    [bgColorView setBackgroundColor:[UIColor brownColor]]; 
    [cell setSelectedBackgroundView:bgColorView]; 
    return cell; 
} 

每次我點擊一個細胞它被用棕色選擇保持選中狀態,直到我點擊另一個單元格...完美 但是,當我駁回的觀點點擊我的導航控制器的後退按鈕,然後切換回我的視圖,我的單元格不再被選中。那麼如何才能實現在切換視圖之前選中的單元格仍然會在我回到視圖時選中?

我想我可能必須從tableview創建一個屬性,然後再次選擇viewDidLoad中的行。 selectedRow索引我可以保存在nsuserdefaults ..但我希望有一個更簡單的解決方案。

+0

沒有人?請我真的需要你的幫助! – dehlen

回答

0

在你-viewDidLoad方法,這增加了底:self.clearsSelectionOnViewWillAppear = NO;這將使它看起來像這樣:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.clearsSelectionOnViewWillAppear = NO; 
} 
+0

但我沒有uitableviewcontroller,我在uiviewcontroller – dehlen

相關問題