【问题标题】:selectRowAtIndexPath from another UIVIewController not working来自另一个 UIVIewController 的 selectRowAtIndexPath 不起作用
【发布时间】:2014-04-12 01:46:44
【问题描述】:

我正在尝试在 UITableView 上调用 selectRowAtIndexPath,该 UITableView 是我从中调用它的 UIViewController 的子视图。

我已经设置好了,当你选择一个单元格时,它会变成灰色,这很好,但是我正在将不同的数据集加载到 UITableView 中和从 UITableView 中加载出来,并且每当进行选择时,我都会将选定的 NSIndexPath 发送回UIViewController。然后,当视图下一次加载正确的 NSIndexPath 数据集时,我会从 UIViewController 调用此方法。

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

然后在 UITableView 类中,我的 cellForRowAtIndexPath 和 didSelectRowAtIndexPath 看起来像这样。

- (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];

}

当它在屏幕上加载时,正确的单元格将突出显示一秒钟然后变回白色。

更新

cellForRowAtIndexPath 中的新 if 语句

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

我仍然收到同样的错误。

【问题讨论】:

  • 您不应该将 indexPaths 与“==”进行比较,因为它们是对象。你应该使用 isEqual: 代替。
  • 不仅 if 和 else if 都是 == 哈哈错过了那个.. 我更新了它.. 将在几秒钟内显示新代码。

标签: ios objective-c uitableview uiviewcontroller


【解决方案1】:

UITableViewController 有一个名为clearsSelectionOnViewWillAppear 的属性。 From the doc:

当表格视图第一次加载时即将出现时, 表视图控制器重新加载表视图的数据。它也清除 它的选择(有或没有动画,取决于请求) 每次显示表格视图时。 UITableViewController 类在超类方法 viewWillAppear: 中实现了这一点。你 可以通过更改中的值来禁用此行为 清除SelectionOnViewWillAppear 属性。

所以在那个表视图控制器子类中,在 viewDidLoad...

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

【讨论】:

  • 哇..我一直在尝试很多东西,我有点迷失在这一切中......哦,我今天学到了一些新东西,希望下次我能记住它。大声笑非常感谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多