【问题标题】:Is it possible to derive the indexPath if you know the row?如果您知道该行,是否可以派生 indexPath?
【发布时间】:2016-08-24 11:22:32
【问题描述】:

好的,我将尽可能简单地分解它。我在 ViewController 中有一个 tableView。我有两个用于表格的原型单元格。我多次重复使用单元格来填充表格。

在其中一个单元格中,我已将手势识别器添加到label,通过它我使textField 在标签位置可见并隐藏label。现在,当我使用完textField 并按回车键后,我希望标签文本更改为我在textField 中输入的内容。所以我在viewController 中实现了UITextFieldDelegate 协议。我还为单元格中的每个 textField 添加了标签,以便我知道 textField 返回的内容以及 textField 所在的行。

基本上,我想知道的是,如果我已经知道 indexPath.row,是否有任何方法可以获取 indexPath?

对于手势识别器,我可以通过从点击位置获取 indexPath 来解决这个问题:

func genderTapped(sender: UITapGestureRecognizer) {

        let tapLocation = sender.locationInView(self.profileInfoTable)
        let indexPath = self.profileInfoTable.indexPathForRowAtPoint(tapLocation)

        let cell = self.profileInfoTable.cellForRowAtIndexPath(indexPath!) as! editUserDataCell
        cell.savedUserInput.hidden = true
        cell.userDetailTextfield.becomeFirstResponder()
        cell.userDetailTextfield.hidden = false
        cell.userDetailTextfield.text = cell.savedUserInput.text!            
    }

我需要indexPath 以便我可以引用单元格中包含的元素。任何人都可以提供任何见解吗?有没有人尝试过类似的方法?有什么方法可以只使用行来访问单元格吗?

【问题讨论】:

  • 如何设置tag to each of the textFields
  • 要访问 UITableView 行,您总是需要一个部分的索引和该部分中的行的索引。因此,如果您也知道需要访问的部分,它可以有一个简单的答案。如果你的tableView只有一个部分,它的索引当然是0。
  • @pkc456 我在 cellForRowAtIndexPath 函数中设置了标签
  • @pedrouan 我只维护一个部分
  • 我的建议是将标签更改为按钮,并为其添加目标以隐藏或显示文本字段。然后将UITextFieldDelegate 设置为您的单元格。如果您想与 tableView 交流您的单元格。将控制器的弱引用添加到单元类或使用闭包是不错的选择。

标签: ios swift uitableview nsindexpath


【解决方案1】:

如果您能够在GestureMethod 中获取indexPath,那么您可以创建一个NSIndexPath 类型的实例属性将其值存储在该手势的方法中,然后在textFieldShouldReturn 委托方法中使用indexPath,类似这样.

var selectedIndexPath: NSIndexPath?

func genderTapped(sender: UITapGestureRecognizer) {

    let tapLocation = sender.locationInView(self.profileInfoTable)
    self.selectedIndexPath = self.profileInfoTable.indexPathForRowAtPoint(tapLocation)

    let cell = self.profileInfoTable.cellForRowAtIndexPath(self.selectedIndexPath!) as! editUserDataCell
    cell.savedUserInput.hidden = true
    cell.userDetailTextfield.becomeFirstResponder()
    cell.userDetailTextfield.hidden = false
    cell.userDetailTextfield.text = cell.savedUserInput.text!
} 

现在在UITextFieldDelegate 方法中使用这个self.selectedIndexPath

编辑:从您的问题评论中,您告诉您只有一个Section,因此您也可以通过这种方式从textFieldtag 创建indexPath。 p>

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let indexPath = NSIndexPath(forRow: textField.tag, inSection: 0)
    //Or You can use self.selectedIndexPath also 
}

【讨论】:

    【解决方案2】:

    如果是单个或多个部分,下面的代码将起作用

    在您的cellForRowAtIndexPath 中,设置标签如下:-

    let tag = indexPath.section*100 + indexPath.row
    
    cell.savedUserInput.tag = tag
    cell.userDetailTextfield.tag = tag
    

    在您的文本字段委托方法中,获取 indexPath 如下:-

    func genderTapped(sender: UITapGestureRecognizer) {
    let textfieldObject = sender as! UITextField 
    let sectionTag = textfieldObject.tag % 100
    let rowTag = textfieldObject.tag / 100
    
    let indexPath = NSIndexPath(forRow: rowTag.tag, inSection: sectionTag)
    }
    

    【讨论】:

      【解决方案3】:

      免责声明:这不是对此处提出的字面问题的回答,但它可能为 OP 的目标提供更简单的解决方案。

      除非除了你在问题中描述的内容之外,你还需要做一些事情,否则在我看来,一个更简单的解决方案是根本不使用标签,而是只需使用 UITextField 并将其设置为 enabled当您希望它像标签一样时,属性为 false。

      如果您需要在模式更改时更改样式,则可以将UITextField 子类化。

      【讨论】:

      • 有趣,我没有想到这种方法。
      【解决方案4】:

      如果您知道要访问的行号以及该行所在的部分,请使用此代码

      let indexPath = NSIndexPath(forRow: row, inSection: section)
      

      要访问此indexPath对应的单元格,请使用

      let cell = tableView.cellForRowAtIndexPath(indexPath) as! tableViewCell
      

      【讨论】:

        猜你喜欢
        • 2011-12-25
        • 1970-01-01
        • 2020-08-26
        • 2014-02-08
        • 2016-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多