获取UITableView的选定索引

时间:2010-10-27 07:18:39

标签: ios objective-c iphone uitableview nsindexpath

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

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

这始终适用于第1项。我想在indexPathForRow.

中选择索引

请帮忙。 感谢。

5 个答案:

答案 0 :(得分:214)

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

答案 1 :(得分:8)

获取当前选定的行。 如果您只有一个部分,可能会有所帮助。

- (NSUInteger)currentSellectedRowIndex
{
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    if (selectedIndexPath) {
        return selectedIndexPath.row;
    }
    else {
        return NSNotFound;
    }
}

答案 2 :(得分:5)

使用此代码

CGPoint location =[sender locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell  = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:swipedCell];

答案 3 :(得分:4)

在didSelectRowAtIndexPath中,将声明为NSIndexPath的接口设置为返回的索引路径。然后您可以滚动到该变量。如果您需要帮助,请发表评论并提供一些示例代码。

答案 4 :(得分:1)

Swift 4.2

let selectedIndexPath = tableView.indexPathForSelectedRow

这是一个可选的返回值。

相关问题