获取 UITableView 的选定索引

我想为 UITableView选择索引。 我写了以下代码:

NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
[tableView scrollToRowAtIndexPath:index
atScrollPosition:UITableViewScrollPositionTop
animated:YES];

这总是工作的第一个项目。我想在 indexPathForRow.中选择索引

请帮帮我。 谢谢。

85422 次浏览

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.

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

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

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;
}
}

Swift 4.2

let selectedIndexPath = tableView.indexPathForSelectedRow

which is an optional return value.