【问题标题】:Manually call didSelectRowatIndexPath手动调用 didSelectRowatIndexPath
【发布时间】:2013-08-17 05:09:17
【问题描述】:

我正在尝试以编程方式调用 didSelectRowAtIndexPath,但遇到了问题。

[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];

给我以下错误:

使用未声明的标识符“indexPath”;你的意思是“NSIndexPath”吗?

谁能帮帮我?谢谢!

编辑:从下面的回复看来,我的做法是错误的。按下按钮时如何获取所选项目的文本(可以是多项选择)?我需要在专用于按钮按下的函数中执行此操作。

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    只是为了更新@Sourabh's answer,注意你也可以在调用selectRow时提供scrollPosition

    let indexPath = IndexPath(row: 7, section: 0)
    tblView.selectRow(at: indexPath, animated: true)
    tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
    

    变成:

    let indexPath = IndexPath(row: 7, section: 0)
    tblView.selectRow(at: indexPath, animated: true, scrollPosition: .top) // <--
    tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
    

    possible constants 是:

    .none

    表格视图滚动感兴趣的行以完全可见 最小的运动。如果该行已经完全可见,则不滚动 发生。例如,如果该行位于可见区域上方,则 行为与 top 指定的行为相同。这是默认设置。

    .top

    表格视图将感兴趣的行滚动到可见的顶部 表格视图。

    .middle

    表格视图将感兴趣的行滚动到中间 可见的表格视图。

    .bottom

    表格视图将感兴趣的行滚动到底部 可见的表格视图。

    【讨论】:

      【解决方案2】:

      Mistalis answer 开始,我制作了这个ExtendedCell 小类,因为我在几个表视图中使用它。

      在表格视图中的使用:

      // Here MyTableView is UITableView and UITableViewDelegate
      // but you can separate them.
      
      class MyTableView: UITableView, UITableViewDelegate {
      
          override func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? {
              return MyCell(identifier: identifier, table: self)
          }
      
          // ...
      }
      

      你的单元的实现:

      class MyCell : ExtendedCell {
      
          convenience init(identifier: String?, table: MyTableView)
          {
              // in this example, MyTableView is both table and delegate
              self.init(identifier: identifier, table: table, delegate: table)
      
              // ...
          }
      
          // At any moment you may call selectTableRow() to select
          // the row associated to this cell.
          func viewItemDetail() {
            selectTableRow()
          }
      }
      

      ExtendedCell类:

      import UIKit
      
      /**
       * UITableViewCell with extended functionality like
       * the ability to trigger a selected row on the table
       */
      class ExtendedCell : UITableViewCell {
      
          // We store the table and delegate to trigger the selected cell
          // See: https://stackoverflow.com/q/18245441/manually-call-did-select-row-at-index-path
      
          weak var table : UITableView!
          weak var delegate : UITableViewDelegate!
      
          convenience init(identifier: String?, table: UITableView, delegate: UITableViewDelegate)
          {
              self.init(style: .default, reuseIdentifier: identifier)
      
              self.table = table
              self.delegate = delegate
          }
      
          func selectTableRow()
          {
              if let indexPath = table.indexPath(for: self) {
                  table.selectRow(at: indexPath, animated: true, scrollPosition: .none)
                  delegate.tableView!(table, didSelectRowAt: indexPath)
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        Swift 3.0 解决方案

        手动调用 didSelectRowAtIndexPath

        let indexPath = IndexPath(row: 7, section: 0)
        tblView.selectRow(at: indexPath, animated: true)
        tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
        

        【讨论】:

        • @Strange 欢迎,请你告诉我滚动位置中间的变化吗?为什么需要 let indexPath = IndexPath(row: 7, section: 0) tblView.selectRow(at: indexPath, animated: true, scrollPosition: .middle) tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)跨度>
        • @SourabhSharma 看到新函数要求 UITableViewScrollPosition 所以你需要提供任何一个可用的选项,我给了 middle 以便所选行可以滚动到 UITableView 的中间。
        【解决方案4】:

        你需要传递一个有效的参数,如果你没有在调用范围内声明indexPath,那么你会得到那个错误。试试:

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:ROW_YOU_WANT_TO_SELECT inSection:SECTION_YOU_WANT_TO_SELECT]
        [self tableView:playListTbl didSelectRowAtIndexPath:indexPath];
        

        ROW_YOU_WANT... 将替换为您希望选择的行和部分。

        但是,您真的不应该直接调用它。将tableView:didSelectRowAtIndexPath: 中正在完成的工作提取到单独的方法中并直接调用它们。

        要解决更新后的问题,您需要对UITableView 使用indexPathsForSelectedRows 方法。想象一下,您正在从字符串数组中填充表格单元格文本,如下所示:

        - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {
                UITableViewCell *cell = [tv dequeue...];
                NSArray *rowsForSection = self.sectionsArray[indexPath.section];
                NSString *textForRow = rowsForSection[indexPath.row];
                cell.textLabel.text = textForRow;
                return cell;
            }
        

        然后,要获取所有选定的文本,您需要执行以下操作:

        NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
        NSMutableArray *selectedTexts = [NSMutableArray array];
        for (NSIndexPath *indexPath in selectedIndexPaths) {
            NSArray *section = self.sectionsArray[indexPath.section];
            NSString *text = section[indexPath.row];
            [selectedTexts addObject:text];
        }
        

        selectedTexts 此时将包含所有选定的信息。希望这个例子有意义。

        【讨论】:

        • 看起来我肯定不会以正确的方式使用我的方法,基本上我想在单击按钮时获取 UITableView 中所选项目的所有文本。我会修改我原来的帖子。可能是另一个主题的问题,但我会检查这个作为这个问题的答案。
        • @johnhannigan 好的,这是一个完全不同的问题。
        • 是的,但谢谢你,我会检查这个作为这个答案的答案
        【解决方案5】:

        如果您还没有要插入的 indexPath 变量,则需要定义一个变量。

        类似:

            NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-15
          • 2010-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-08
          相关资源
          最近更新 更多