ios - 未调用 selectRowAtIndexPath

标签 ios objective-c uitableview asynchronous

我有一个类,允许用户将条目添加到服务器端表。一切正常,直到我尝试刷新 UITableView与新数据。我调用服务器来获取新数据集,用它来刷新 NSArray那是表的数据源,然后尝试重新加载表。这是从服务器返回数据时调用的方法:

- (void) logEntriesRefreshed : (NSNotification *) notification {

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"log_entries_refreshed"
                                                  object:nil];

    NSLog(@"returned from log entries fetch");

    _logEntriesArray = [LogEntriesDataFetcher getLogEntriesArray];
    [_tableView reloadData];

    _activityIndicator.hidden = YES;
    [_activityIndicator stopAnimating];

    NSLog(@"log entries array count: %lu", [_logEntriesArray count]);

    [_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                            animated:NO
                      scrollPosition:UITableViewScrollPositionNone];
}

问题出在最后一行。我想以编程方式选择表中的第一行(必须至少有一个,因为我刚刚添加了一行)。但似乎这条线永远不会执行。注意这个方法,接下来应该是:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"here");

    UITableViewCell *previousCell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:_previousIndexPath];
    previousCell.backgroundColor = [UIColor clearColor];
    previousCell.textLabel.textColor = [SharedVisualElements primaryFontColor];

    UITableViewCell *cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [SharedVisualElements secondaryFontColor];
    cell.textLabel.textColor = [SharedVisualElements primaryFontColor];

    _previousIndexPath = indexPath;

    // get the file attributes for the cell just selected
    _currentEntry = (LogEntry *)[_logEntriesArray objectAtIndex:[indexPath row]];

    NSLog(@"array count: %lu", (unsigned long)[_logEntriesArray count]);
    NSLog(@"current entry: %ld", (long)[indexPath row]);

    _isExistingEntry = YES;
    _arrayPositionOfEntryBeingEdited = [indexPath row];

    [self initializeValues];
    [self initializeObjects];
    [self captureStartingValuesForStateMachine];
}

我在 selectRowAtIndexPath 上设置了断点行和第一个 NSLog(@"here")输入 didSelectRow... .我到达 selectRowAtIndexPath行,但从不到 didSelectRow方法。我的控制台输出与此一致:
returned from log entries fetch
log entries array count: 7

这就是它的结束。 didSelectRow... 中没有任何内容方法。也没有抛出错误。

我错过了什么。看起来很简单,但我做的任何事情似乎都不起作用。

最佳答案

根据 Apple 的文档,调用 selectRowAtIndexPath不会调用 didSelectRowAtIndexPath .看看here .

Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor does it send UITableViewSelectionDidChangeNotification notifications to observers.



要专门调用 didSelectRowAtIndexPath委托(delegate)方法,使用以下代码:
[[tableView delegate] tableView:tableView didSelectRowAtIndexPath:indexPath];

希望这可以帮助。

关于ios - 未调用 selectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076443/

相关文章:

ios - 添加 JSON 数据时 UITableView 不工作

ios - 如何将委托(delegate)函数转换为 IBAction 函数

ios - 在 UITableviewCell 中布局动态 subview ,对边距进行约束

ios - swift : "mapView.showUserLocation = true"返回 "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) "

ios - 查找IOS中柑橘支付集成问题

objective-c - MobFox iOS 框架,当 XIB 卸载时崩溃......我在这里做错了什么?

objective-c - 如何用文档装饰 Objective C 方法?

iphone - 在 UITableViewCell 上实现我自己的滑动

ios - 在 React-Native 中将 RCTRootView 作为 subview 添加到 rootViewController

ios - 禁用时分隔符出现在 uitableview 中