我是斯威夫特的新手..。我需要将indexpath传递给texfield委托方法。使用“标记”,我只能传递行或段。
我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "celdaConValores", for: indexPath) as! celdaDomestica
switch indexPath.section {
case 1:
cell.titulo.text = checklist.datosDomestica?.titulos[indexPath.row] ?? ""
cell.valor.tag = indexPath.row
cell.valor.delegate = self
return cell
case 2:
cell.titulo.text = checklist.datosFrigoDomestica?.titulos[indexPath.row] ?? ""
cell.valor.tag = indexPath.row
cell.valor.delegate = self
return cell
case 3:
cell.titulo.text = checklist.otrosDatosDomestica?.titulos[indexPath.row] ?? ""
cell.valor.tag = indexPath.row
cell.valor.delegate = self
return cell
default:
return cell
}
func textFieldDidEndEditing(_ textField: UITextField) {
let row = textField.tag
if let texto = textField.text{
¿?need the section!
}
}我见过像在标记值中计算结果这样的答案,但我并不认为它是一个好的实践
谢谢!
发布于 2022-08-08 12:32:34
使用标记是相当脆弱的,一般不建议使用。相反,我建议使用文本字段的框架的坐标来确定包含它的单元格。对UITableView使用以下扩展:
// Created by Duncan Champney on 12/23/16.
// Copyright © 2016-2017 Duncan Champney.
// May be used freely in for any purpose as long as this copyright
// notice is included.
//
import UIKit
public extension UITableView {
/**
This method returns the indexPath of the cell that contains the specified view
- Parameter view: The view to find.
- Returns: The indexPath of the cell containing the view, or nil if it can't be found
*/
func indexPathForView(_ view: UIView) -> IndexPath? {
let center = view.center
//The center of the view is a better point to use, but we can only use it if the view has a superview
guard let superview = view.superview else {
//The view we were passed does not have a valid superview.
//Use the view's bounds.origin and convert from the view's coordinate system
let origin = self.convert(view.bounds.origin, from: view)
let indexPath = self.indexPathForRow(at: origin)
return indexPath
}
let viewCenter = self.convert(center, from: superview)
let indexPath = self.indexPathForRow(at: viewCenter)
return indexPath
}
}因此,您的委托方法将如下所示:
func textFieldDidEndEditing(_ textField: UITextField) {
if let indexPath = tableView.indexPathForView(textField) {
let row = indexPath.row
let section = indexPath.section
} else {
// Can't find the IndexPath. Handle error
}
}您可以在github链接上找到使用该代码的工作应用程序:
发布于 2022-08-08 12:40:28
您通常不希望通过单元格的indexPath或标记来引用它们。如果删除或插入单元格会发生什么?你必须更新所有其他的值。UIKit和标准设计模式允许我们将我们需要的东西与我们的单元联系起来。
您可以做的是在单元格上创建一个委托,该委托传递到文本更改的单元格中。一旦您有了这个单元格,如果需要的话,您可以从indexPath中获得它。
class ViewController: UIViewController {
lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .insetGrouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(SomeCell.self, forCellReuseIdentifier: "SomeCell")
tableView.backgroundColor = .systemBackground
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableView.automaticDimension
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.snp.makeConstraints{ (make) in
make.edges.equalTo(view.safeAreaLayoutGuide)
}
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SomeCell", for: indexPath) as! SomeCell
cell.delegate = self
return cell
}
}
extension ViewController: SomeCellDelegate {
func someCellTextChanged(_ cell: SomeCell, text: String?) {
guard let indexPath = tableView.indexPath(for: cell) else { fatalError("Oh no") }
print("someCellTextChanged at indexPath: \(indexPath) to \(text ?? "")")
}
}
protocol SomeCellDelegate: AnyObject {
func someCellTextChanged(_ cell: SomeCell, text: String?)
}
class SomeCell: UITableViewCell, UITextFieldDelegate {
weak var delegate: SomeCellDelegate?
lazy var textField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.backgroundColor = .systemPurple
return textField
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(textField)
textField.delegate = self
textField.snp.makeConstraints { make in
make.edges.equalTo(contentView.layoutMarginsGuide)
}
backgroundColor = .systemMint
contentView.backgroundColor = .systemPink
}
required init?(coder: NSCoder) {
fatalError("nope")
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
delegate?.someCellTextChanged(self, text: textField.text)
}
}https://stackoverflow.com/questions/73277305
复制相似问题