2016-08-24 88 views
2

好吧,我会试着尽可能简单地打破这一点。我在ViewController中有一个tableView。我有两个桌子原型单元。我正在重复使用这些单元格来填充表格。如果知道该行,是否可以派生indexPath?

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

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

对于手势识别,我能够从抽头位置获得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,这样我可以指包含在细胞内的元素。谁能提供任何见解?有没有人试过类似的方法?有什么方法可以通过使用该行来访问单元格?

+0

如何设置'tag到每个textFields'? – pkc456

+0

对于访问UITableView行,您始终需要该节中的某一节的索引和该行的索引。所以如果你也知道你需要访问的部分,它可以有一个简单的答案。如果你只有一个部分它的tableView,它的索引当然是0. – pedrouan

+0

@ pkc456我在cellForRowAtIndexPath函数中设置标签 – Jobs

回答

2

如果能够得到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.selectedIndexPathUITextFieldDelegate方法。

编辑:从你的问题的评论你告诉你只有一个Section所以你也可以创建从textFieldtag这样indexPath

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

如果你知道你正在访问的行号,并在该行的部分,然后用这个代码

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

对于访问与本indexPath细胞,使用

let cell = tableView.cellForRowAtIndexPath(indexPath) as! tableViewCell 
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) 
} 
1

免责声明:这不是一个答案到这里提到的字面问题,但它可能为OP的目标提供更简单的解决方案。

除非你需要做的除了东西给你在你的问题描述了,在我看来,一个更容易的解决办法是完全不使用标签,但在代替只需使用一个UITextField并设置它的enabled属性当你希望它像一个标签一样行事时就是假的。

如果在模式更改时需要更改样式,则可以继承UITextField

+0

有趣的是,我没有想到这种方法。 – Jobs

相关问题