Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 以编程方式选择tableview行_Iphone_Objective C_Cocoa Touch_Uitableview - Fatal编程技术网

Iphone 以编程方式选择tableview行

Iphone 以编程方式选择tableview行,iphone,objective-c,cocoa-touch,uitableview,Iphone,Objective C,Cocoa Touch,Uitableview,如何以编程方式选择UITableView行,以便 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 被处决选择行索引路径将仅高亮显示该行 UITableView应该可以做到这一点 只需传递scrollPosition的UITableViewScrollPositionNone,用户将看不到任何移动 您还应该能够手动运行操作: [tableVi

如何以编程方式选择
UITableView
行,以便

- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath
被处决<代码>选择行索引路径将仅高亮显示该行

UITableView应该可以做到这一点

只需传递scrollPosition的
UITableViewScrollPositionNone
,用户将看不到任何移动


您还应该能够手动运行操作:

[tableView.delegate tableView:tableView未选择rowatindexpath:indexath]

选择Rowatindexpath:animated:scrollPosition:
之后,高亮显示以及任何相关逻辑都会出现。

来自

调用此方法不会导致代理收到
tableView:willSelectRowAtIndexPath:
tableView:didSelectRowAtIndexPath:
消息,也不会向观察者发送
UITableViewSelectionDidChangeNotification
通知

我要做的是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self doSomethingWithRowAtIndexPath:indexPath];
}

然后,从您想要调用selectRowAtIndexPath的位置,您可以调用doSomethingWithRowAtIndexPath。除此之外,如果您希望UI反馈发生,还可以调用selectRowAtIndexPath。

使用此类别选择表行并在延迟后执行给定的序列。
在您的
viewdide
方法中调用此选项:

[tableViewController delayedSelection:withSegueIdentifier:]


@implementation UITableViewController (TLUtils)

-(void)delayedSelection:(NSIndexPath *)idxPath withSegueIdentifier:(NSString *)segueID {
    if (!idxPath) idxPath = [NSIndexPath indexPathForRow:0 inSection:0];                                                                                                                                                                 
    [self performSelector:@selector(selectIndexPath:) withObject:@{@"NSIndexPath": idxPath, @"UIStoryboardSegue": segueID } afterDelay:0];                                                                                               
}

-(void)selectIndexPath:(NSDictionary *)args {
    NSIndexPath *idxPath = args[@"NSIndexPath"];                                                                                                                                                                                         
    [self.tableView selectRowAtIndexPath:idxPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];                                                                                                                            

    if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
        [self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:idxPath];                                                                                                                                              

    [self performSegueWithIdentifier:args[@"UIStoryboardSegue"] sender:self];                                                                                                                                                            
}

@end
正如Jaanus所说:

调用此(-SelectRowatingIndexPath:animated:scrollPosition:)方法 不会导致代理收到 tableView:将选择RowAtIndexPath:或 tableView:didSelectRowAtIndexPath:消息,也不会发送 UITableViewSelectionDidChangeNotification通知观察者

因此,您只需自己调用
委托
方法即可

例如:

Swift 3版本:

let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)
 let indexPath = NSIndexPath(forRow: 0, inSection: 0);
 self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
 self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)
ObjectiveC版本:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath 
                            animated:YES 
                      scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
Swift 2.3版本:

let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)
 let indexPath = NSIndexPath(forRow: 0, inSection: 0);
 self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
 self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)

iPad和iPhone平台有两种不同的方法,因此您需要同时实现这两种方法:

  • 选择处理程序和
  • 塞格

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    
    // Selection handler (for horizontal iPad)
    [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    
    // Segue (for iPhone and vertical iPad)
    [self performSegueWithIdentifier:"showDetail" sender:self];
    

如果您想选择某一行,这将帮助您

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[someTableView selectRowAtIndexPath:indexPath 
                           animated:NO 
                     scrollPosition:UITableViewScrollPositionNone];
这也将突出显示该行。 然后委派

 [someTableView.delegate someTableView didSelectRowAtIndexPath:indexPath];

Swift 3/4/5解决方案

选择行

取消选择行


对不起,我用的就是这个。我不小心把ScrollToRowatineXpath放了进去。我已经更新了这个问题。ScrollToRowatineXpath只突出显示单元格。是的,对不起。它说它不会在我发布的文档链接中触发didselectrowatinexpath。我需要学习阅读。@anq:非常感谢!当我在自定义单元格中调用DidSelectRowatineXpath:indexPath时,它对我很有帮助。或者当你以编程方式选择行时,你可以调用tableView:DidSelectRowatineXpath:yourself(在你作为委托连接的类中)。这个方法对我来说似乎是一种攻击,它确实完成了任务,但我认为应该有更好的方法。我真的不认为您需要创建一个调用“doSomethingWithRowAtIndexPath”难道你不能直接调用委托方法并传递从你实现委托方法的地方执行didSelect逻辑所需的参数吗?我遇到了同样的问题,只是修改了链接:我希望这对你有帮助。这个问题与segues无关。是的,你的回答中没有任何与问题相关的东西。请欣赏这个答案。如果用户希望实现相同的行为,但使用的是故事板,那么这种方法非常有效。对我来说,添加“.delegate”是关键。(Swift 4.5)