UITableViewCell在切换视图时显示选定项

UITableViewCell在切换视图时显示选定项,uitableview,selection,highlight,Uitableview,Selection,Highlight,我得到了一个UITableViewCell,单击它时我想保持选中状态。所以我有一个代码,它工作得很好: - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReus

我得到了一个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;
}
每次我点击一个单元格,它就会被选为棕色,并保持选中状态,直到我点击另一个单元格。。。完美的 但当我通过单击navigationcontroller的后退按钮关闭视图,然后切换回视图时,我的单元格不再被选中。那么,当我返回视图时,如何实现在切换视图之前选择的单元格仍然处于选中状态


我想我可能需要从tableview创建一个属性,然后再次选择ViewDiLoad中的行。可以保存在nsuserdefaults中的selectedRow索引。。但我希望有一个更简单的解决方案。

在您的
-viewDidLoad
方法中,将此添加到底部:
self.clearselectiononviewwillappear=NO这将使它看起来像这样:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.clearsSelectionOnViewWillAppear = NO;
}

没有人求你了,我真的需要你的帮助!但是我没有uitableviewcontroller,我在uiviewcontroller中