UITableViewがあり、プロトタイプセルはラベルとTextFieldで構成されています。関数func1、func2、fun3、...を含むクラスMyClassもあります。同じtableViewCellプロトタイプを使用するViewControllerがいくつかあります。各viewControllerには、inst1、inst2、およびinst3と呼ばれるMyClassのインスタンスがあります。FirstViewControllerのTableViewにテキストを入力するとき、各行がその行に対応するMyClassのインスタンスから関数を呼び出すようにします。
したがって、FirstViewControllerの行1にテキストを入力するときに、textFieldに入力したデータをinst1のfunc1に渡します。FirstViewControllerの行2にデータを入力するときに、テキストフィールドのデータをinst1のfunc2に渡す必要があります。などなど、行を下に移動します。
私はこれに非常に慣れていないので、これを行う方法を理解するのに助けていただければ幸いです。それが意味をなさない場合はお知らせください。言い換えることができます。私は本当にこれについて助けが必要です。前もって感謝します!
*私のコードを表示するように質問を更新しました
以下は私のコードです:FirstViewController.swift
extension FirstViewController: MyCellDelegate {
func MyCell(_ cell: UITableViewCell, didEnterText text: String) {
if let indexPath = tableView.indexPath(for: cell) {
if (indexPath.hashValue == 0) {
inst1.func1(one: text)
}
if (indexPath.hashValue == 1) {
inst1.func2(two: text)
}
}
totalText.text = inst1.getMyTotal()
}
}
import UIKit
class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let inst1 = MyClass()
@IBOutlet weak var totalText: UILabel!
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 11
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myTableCell") as! TableViewCell
let text = cell.cellData[indexPath.row]
cell.myTextField.tag = indexPath.row
cell.delegate = self
cell.myLabel.text = text
cell.myTextField.placeholder = text
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
TableViewCell.swift
import UIKit
protocol MyCellDelegate: class {
func MyCell(_ cell: UITableViewCell, didEnterText text: String)
}
class TableViewCell: UITableViewCell {
weak var delegate: MyCellDelegate?
public var cellData: [String] = ["1","2","3","4","5","6","7","8","9","10","11"]
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myTextField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
}
}
FirstViewController拡張機能にブレークポイントを設定すると、そのコードは実行されません。
でWillDisplayCellのUITextFieldにタグを追加します。また、対応するviewControllerに通知するプロトコルを作成し、ここで自分自身をデリゲートとして設定します。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")
cell.textField.tag = indexPath.row
cell.delegate = self
}
セルクラスのプロトコルは次のようになります
protocol MyCellDelegate: class {
func MyCell(_ cell: UITableViewCell, didEnterText text: String)
}
class MyCell: UITableViewCell, UITextFieldDelegate {
weak var delegate: MyCellDelegate?
override fun awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
//All the remaining code goes here
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
delegate?.MyCell(self, didEnterText: textField.text! )
return true
}
}
デリゲートに準拠しているFirstViewControllerで、これを実行します。
extension FirstViewController: MyCellDelegate {
func MyCell(_ cell: UITableViewCell, didEnterText text: String) {
if let indexPath = tableView.indexPathForCell(cell) {
// call whichever method you want to call based on index path
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加