Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果知道行,是否可以派生indexPath?_Ios_Swift_Uitableview_Nsindexpath - Fatal编程技术网

Ios 如果知道行,是否可以派生indexPath?

Ios 如果知道行,是否可以派生indexPath?,ios,swift,uitableview,nsindexpath,Ios,Swift,Uitableview,Nsindexpath,好的,我会尽可能简单地把它分解。我在ViewController中有一个tableView。我有两个原型细胞的表。我多次重复使用单元格来填充表 在其中一个单元格中,我将手势识别器添加到标签,通过它,我可以在标签的位置上显示文本字段,并隐藏标签。现在,当我使用textField并按下返回键时,我希望标签文本更改为我在textField中输入的内容。因此,我在viewController中实现了UITextFieldDelegate协议。我还向单元格中的每个文本字段添加了标记,以便知道textFie

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

在其中一个单元格中,我将手势识别器添加到
标签
,通过它,我可以在标签的位置上显示
文本字段
,并隐藏
标签
。现在,当我使用
textField
并按下返回键时,我希望标签文本更改为我在
textField
中输入的内容。因此,我在
viewController
中实现了
UITextFieldDelegate
协议。我还向单元格中的每个文本字段添加了标记,以便知道
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
,以便可以引用单元格中包含的元素。有人能提供一些见解吗?有人尝试过类似的方法吗?是否有任何方法可以通过使用行访问单元格?

如果您能够在
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!
} 
现在使用这个
self.selectedIndexPath
inside
UITextFieldDelegate
方法

编辑:根据您的问题注释,您已经告知您只有一个
部分
,因此您也可以通过这种方式从
文本字段
标记
创建
索引XPath

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

如果您知道要访问的行号和该行所在的节,则使用此代码

let indexPath = NSIndexPath(forRow: row, inSection: section)
要访问与此索引对应的单元格,请使用

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

如果是单节或多节,以下代码将起作用

单元格中,按如下所示设置标签:-

let tag = indexPath.section*100 + indexPath.row

cell.savedUserInput.tag = tag
cell.userDetailTextfield.tag = tag
在textfield委托方法中,按如下方式获取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)
}
免责声明:这不是对这里提出的字面问题的回答,但它可能为OP的目标提供一个更简单的解决方案

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


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

如何将
标记设置到每个文本字段
?要访问UITableView行,始终需要一个节的索引和该节中的一行的索引。因此,如果您的用户也知道您需要访问的部分,那么它可以有一个简单的答案。如果在tableView中只有一个部分,那么它的索引当然是0。@pkc456我在cellForRowAtIndexPath中设置了标记function@pedrouan我只是维护了一个部分,我的建议是将标签改为按钮,并向其添加目标以隐藏或显示文本字段。然后将
UITextFieldDelegate
设置到单元格。如果要将单元格与tableView通信。将控制器的弱引用添加到cell类或使用闭包是一个不错的选择。有趣的是,我没有想到这种方法。