2017-11-25 7 views
0

私はtableViewにテキストフィールドを持っています。私はテキストフィールドの位置を取得する必要がありますが、問題はそこに複数のセクションがあるということです。 textfield.tagを使ってセクションまたは行を1つだけ取得できますが、両方が必要です。テーブルビューのテキストフィールドのセクション数と行数を取得する方法は?

+0

私は、[here](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-html-html)のアプローチと同様の委譲パターンまたはクロージャコールバックを使用することをお勧めします。 a-button-in-a-cell-is-tap/38941510#38941510)。タグを使用するかビュー階層を歩くことは "嫌な"です – Paulw11

+0

ボタンはありませんそのテキストフィールド –

+0

だから、私は同じように言っています* - ビューコントローラにイベントを戻すために委譲またはコールバッククロージャを使用します。タグを使用したり、階層ウォーキングを表示したりしないでください。 – Paulw11

答えて

0

UIResponderチェーンを歩くことによって、どのクラスの親UIResponderを見つけることができます。 UITextFieldのとUITableViewCellの両方はそう、あなたのテキストフィールドに、この関数を呼び出すことができ、あなたのテキストフィールドの親tableViewCellを取得するには、UIResponderから継承UIViewの、継承:

extension UIResponder { 
    func findParentTableViewCell() -> UITableViewCell? { 
     var parent: UIResponder = self 
     while let next = parent.next { 
      if let tableViewCell = parent as? UITableViewCell { 
       return tableViewCell 
      } 
      parent = next 
     } 
     return nil 
    } 
} 

あなたはtableViewCellを持っていたら次に、あなただけのtableViewを頼みますtableView.indexPAth(for:)

とそのインデックスパスのためにあなたは、タグフィールドを使用する必要はありません:

guard let cell = textField.findParentTableViewCell(), 
     let indexPath = tableView.indexPath(for: cell) else { 
     print("This textfield is not in the tableview!") 
} 
print("The indexPath is \(indexPath)") 
0

あなたは、私が書いたprevious answerのバリエーションを使用することができます。

セルとテーブルビューの間でデリゲートプロトコルを使用します。これにより、セルサブクラスにテキストフィールドデリゲートを維持できます。これにより、ビジネスロジックをビューコントローラに保持したまま、Interface Builderのプロトタイプセルにタッチテキストフィールドデリゲートを割り当てることができます。

ビューの階層をナビゲートする可能性のある脆弱な方法や、(挿入、削除または並べ替えの結果として)セルインデックスが変更されたときに問題が発生する、tagプロパティの使用を回避します。ここでは、セクション番号と行番号を知る必要があります。

CellSubclass.swift

protocol CellSubclassDelegate: class { 
    func textFieldUpdatedInCell(_ cell: CellSubclass) 
} 

class CellSubclass: UITableViewCell { 

@IBOutlet var someTextField: UITextField! 

var delegate: CellSubclassDelegate? 

override func prepareForReuse() { 
    super.prepareForReuse() 
    self.delegate = nil 
} 
func textFieldShouldReturn(_ textField: UITextField) -> Bool 
    self.delegate?.textFieldUpdatedInCell(self) 
    return yes 
} 

ViewController.swift

class MyViewController: UIViewController, CellSubclassDelegate { 

    @IBOutlet var tableview: UITableView! 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellSubclass 

     cell.delegate = self 

     // Other cell setup 

    } 

    // MARK: CellSubclassDelegate 

    func textFieldUpdatedInCell(_ cell: CellSubclass) { 
     guard let indexPath = self.tableView.indexPathForCell(cell) else { 
      // Note, this shouldn't happen - how did the user tap on a button that wasn't on screen? 
      return 
     } 

     // Do whatever you need to do with the indexPath 

     print("Text field updated on row \(indexPath.row) of section \(indexPath.section") 
    } 
} 

また、閉鎖ではなく、同じ質問でデリゲートパターンを使用してヤコブ王の答えを見ることができます。

関連する問題