ios 获取 UITableView 的选定索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4030811/
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
Get Selected index of UITableView
提问by iOSDev
I want to have selected index for UITableView
.
I have written following code:
我想为UITableView
. 我写了以下代码:
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
[tableView scrollToRowAtIndexPath:index
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
This always work for 1st item. I want to have selected index in indexPathForRow.
这始终适用于第一项。我想选择索引indexPathForRow.
Please help. Thanks.
请帮忙。谢谢。
回答by Chris Gummer
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
回答by ZevsVU
Get current selected row. May be helpful if you have only one section.
获取当前选定的行。如果您只有一个部分,可能会有所帮助。
- (NSUInteger)currentSellectedRowIndex
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedIndexPath) {
return selectedIndexPath.row;
}
else {
return NSNotFound;
}
}
回答by Vineesh TP
Use this code
使用此代码
CGPoint location =[sender locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:swipedCell];
回答by Kolin Krewinkel
In didSelectRowAtIndexPath, set an interface declared NSIndexPath as the indexpath returned. Then you can scroll to that variable. If you need help, comment and I can give some sample code.
在 didSelectRowAtIndexPath 中,设置一个声明为 NSIndexPath 的接口作为返回的索引路径。然后您可以滚动到该变量。如果您需要帮助,请发表评论,我可以提供一些示例代码。
回答by davidrynn
Swift 4.2
斯威夫特 4.2
let selectedIndexPath = tableView.indexPathForSelectedRow
which is an optional return value.
这是一个可选的返回值。