objective-c 以编程方式选择 tableview 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2035061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:43:17  来源:igfitidea点击:

Select tableview row programmatically

iphoneobjective-ccocoa-touchuitableview

提问by 4thSpace

How do I programmatically select a UITableViewrow so that

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

- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath

gets executed? selectRowAtIndexPathwill only highlight the row.

被处决?selectRowAtIndexPath只会突出显示该行。

采纳答案by Jaanus

From reference documentation:

参考文档:

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

调用此方法不会导致委托接收 atableView:willSelectRowAtIndexPath:tableView:didSelectRowAtIndexPath:消息,也不会UITableViewSelectionDidChangeNotification向观察者发送通知。

What I would do is:

我会做的是:

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

And then, from where you wanted to call selectRowAtIndexPath, you instead call doSomethingWithRowAtIndexPath. On top of that, you can additionally also call selectRowAtIndexPath if you want the UI feedback to happen.

然后,从您想要调用 selectRowAtIndexPath 的位置改为调用 doSomethingWithRowAtIndexPath。最重要的是,如果您希望 UI 反馈发生,您还可以另外调用 selectRowAtIndexPath。

回答by dulgan

Like Jaanus told:

就像 Jaanus 所说:

Calling this (-selectRowAtIndexPath:animated:scrollPosition:) method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.

调用这个 (-selectRowAtIndexPath:animated:scrollPosition:) 方法不会导致委托接收 tableView:willSelectRowAtIndexPath: 或 tableView:didSelectRowAtIndexPath: 消息,也不会向观察者发送 UITableViewSelectionDidChangeNotification 通知。

So you just have to call the delegatemethod yourself.

所以你只需要自己调用这个delegate方法。

For example:

例如:

Swift 3 version:

斯威夫特 3 版本:

let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)

ObjectiveC version:

目标C版本:

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

Swift 2.3 version:

斯威夫特 2.3 版本:

 let indexPath = NSIndexPath(forRow: 0, inSection: 0);
 self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
 self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)

回答by anq

UITableView's selectRowAtIndexPath:animated:scrollPosition:should do the trick.

UITableView 的selectRowAtIndexPath:animated:scrollPosition:应该可以解决问题。

Just pass UITableViewScrollPositionNonefor scrollPosition and the user won't see any movement.

只需传递UITableViewScrollPositionNonescrollPosition ,用户就不会看到任何移动。



You should also be able to manually run the action:

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

[theTableView.delegate tableView:theTableView didSelectRowAtIndexPath:indexPath]

[theTableView.delegate tableView:theTableView didSelectRowAtIndexPath:indexPath]

after you selectRowAtIndexPath:animated:scrollPosition:so the highlight happens as well as any associated logic.

在你之后selectRowAtIndexPath:animated:scrollPosition:,突出显示以及任何相关的逻辑发生。

回答by Nazir

if you want to select some row this will help you

如果您想选择某行,这将对您有所帮助

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[someTableView selectRowAtIndexPath:indexPath 
                           animated:NO 
                     scrollPosition:UITableViewScrollPositionNone];

This will also Highlighted the row. Then delegate

这也将突出显示该行。然后委托

 [someTableView.delegate someTableView didSelectRowAtIndexPath:indexPath];

回答by Sourabh Sharma

Swift 3/4/5 Solution

Swift 3/4/5 解决方案

Select Row

选择行

let indexPath = IndexPath(row: 0, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath)

DeSelect Row

取消选择行

let deselectIndexPath = IndexPath(row: 7, section: 0)
tblView.deselectRow(at: deselectIndexPath, animated: true)
tblView.delegate?.tableView!(tblView, didDeselectRowAt: indexPath)

回答by Alexander Volkov

There are two different methods for iPad and iPhone platforms, so you need to implement both:

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

  • selection handler and
  • segue.

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

回答by Jonathan Kolyer

Use this category to select a table row and execute a given segue after a delay.
Call this within your viewDidAppearmethod:

使用此类别选择表行并在延迟后执行给定的 segue。
在您的viewDidAppear方法中调用它:

[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