我有一个类,允许用户将条目添加到服务器端表。一切正常,直到我尝试刷新 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/