【问题标题】:UITableViewCell show selected when switching ViewsUITableViewCell 在切换视图时显示选中
【发布时间】:2012-10-08 20:51:01
【问题描述】:

我有一个 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 中。但我希望有一个更简单的解决方案。

【问题讨论】:

  • 没人?请问我真的需要你的帮助!

标签: uitableview selection highlight


【解决方案1】:

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

- (void)viewDidLoad {
    [super viewDidLoad];

    self.clearsSelectionOnViewWillAppear = NO;
}

【讨论】:

  • 但我没有 uitableviewcontroller,我在 uiviewcontroller 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多