【问题标题】:Get Selected index of UITableView获取 UITableView 的 Selected 索引
【发布时间】:2011-05-01 03:45:44
【问题描述】:

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

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

这始终适用于第一项。我想在indexPathForRow.中选择索引

请帮忙。 谢谢。

【问题讨论】:

    标签: ios objective-c iphone uitableview nsindexpath


    【解决方案1】:
    NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
    

    【讨论】:

    • 如果您允许多选,请考虑使用:- (NSArray *)indexPathsForSelectedRows
    • 一旦你有了*selectedIndexPath,当你需要特定的选择时,我可以访问long pathRow = [selectedIndexPath row];long pathSection = [selectedIndexPath section];(注意NSInteger是一个typedef long,它来自C 背景更有意义,因为无论如何都需要%ld
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      使用此代码

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 或者,您可以使用 Chris 的方法来更快地使用。上述方法提供了更多控制,并且可以由您的(示例)详细视图控制器进行修改。
        【解决方案5】:

        斯威夫特 4.2

        let selectedIndexPath = tableView.indexPathForSelectedRow
        

        这是一个可选的返回值。

        【讨论】:

        • 简单明了。谢谢!
        猜你喜欢
        • 2016-01-10
        • 1970-01-01
        • 2017-02-01
        • 2012-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多