2010-10-27 53 views
95

我想有选择索引UITableView。 我已经写了下面的代码:获取所选的索引的UITableView

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

这总是第一个项目工作。我想在indexPathForRow.

请帮忙。 谢谢。

回答

201
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 
+5

如果您允许多个选择,请考虑使用: - (NSArray *)indexPathsForSelectedRows – yura 2015-02-19 19:12:32

+2

不再有效 – 2015-03-05 06:02:33

+0

@Codecracker我测试了代码。它仍然有效。 – yuchaozh 2016-05-23 21:16:51

4

在didSelectRowAtIndexPath中,将声明NSIndexPath的接口设置为返回的索引路径。然后你可以滚动到那个变量。如果你需要帮助,评论和我可以给一些示例代码。

+0

是请张贴一些示例代码 – iOSDev 2010-10-27 07:25:14

+4

示例代码:http://pastie.org/1254081 – 2010-10-27 23:02:58

+0

或者,您可以用克里斯的方法,以便更快的使用。上面的方法提供了更多的控制,并且可以通过您的(示例)详细视图控制器进行修改。 – 2010-10-27 23:04:28

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

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

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