我有一个使用表格视图的应用程序,每个表格视图单元格中都有两个文本字段cellTitle和cellDescription。我想知道两个文本字段中的一个被编辑时单元格的索引路径是什么。我必须将数据传递给viewController,以便将数据保存在单元格数组中。当我每次测试运行时,self为nil,这将停止代码,并且我无法找到一种方法来让viewController知道已编辑的单元格编号。有人可以帮我找到已编辑文本字段单元格的单元格indexPath吗?
DashboardCell.swift
import UIKit
protocol cellInfoDelegate {
func tableCellInfo(title: String?, description: String?, cell: DashboardCell)
}
class DashboardCell: UITableViewCell {
var indexpathInstance = IndexPath()
//MARK: Outlets
@IBOutlet weak var cellTitle: UITextField!
@IBOutlet weak var cellDescription: UITextField!
@IBAction func cellTitleEdited(_ sender: Any) {
if cellTitle.text != nil {
// ERROR HERE: self is nil
cellInformationDelegate.tableCellInfo(title: cellTitle.text, description: cellDescription.text, cell: self)
} else {
cellTitle.text = "Untitled"
cellInformationDelegate.tableCellInfo(title: cellTitle.text, description: cellDescription.text, cell: self)
}
}
@IBAction func cellDescriptionEdited(_ sender: Any) {
if cellDescription.text != nil {
cellInformationDelegate.tableCellInfo(title: cellTitle.text, description: cellDescription.text, cell: self)
} else {
cellDescription.text = "Add a description Here"
cellInformationDelegate.tableCellInfo(title: cellTitle.text, description: cellDescription.text, cell: self)
}
}
let cellInformationDelegate: cellInfoDelegate! = nil
func setDashboardCell (cell: Dashboard) {
cellTitle.text = cell.title
cellDescription.text = cell.desccription
}
}
DashboardViewcontroller.swift
import UIKit
class DashboardViewController: UIViewController, cellInfoDelegate, UITableViewDataSource, UITableViewDelegate {
var events: [Dashboard] = []
@IBOutlet weak var DashboardAdd: UIButton!
@IBOutlet weak var tableView: UITableView!
@IBAction func DashboardAddA(_ sender: Any) {
events.append(Dashboard(title: "Untitled", description: "Add A Description Here"))
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.hideKeyboardWhenTappedAround()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return events.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Dcells = events[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardCell") as! DashboardCell
cell.setDashboardCell(cell: Dcells)
return cell
}
func tableCellInfo(title: String?, description: String?, cell: DashboardCell) {
let indexPath = (self.tableView.indexPath(for: cell)?.row)!
print("At row \(indexPath), the title is \(title), and the description is \(description)")
if title != "" && description != "" {
events[indexPath] = Dashboard(title: (title)!, description: (description)!)
} else if title != "" && description == "" {
events[indexPath] = Dashboard(title: (title)!, description: "")
} else if title == "" && description != "" {
events[indexPath] = Dashboard(title: "", description: (description)!)
} else {
events[indexPath] = Dashboard(title: "", description: "")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func getCellNumber (cell: DashboardCell) -> Int?{
let indexPath = self.tableView.indexPath(for: cell)
return indexPath?.row
}
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
答案 0 :(得分:-1)
首先,您需要进行更改
let cellInformationDelegate: cellInfoDelegate! = nil
收件人:
var cellInformationDelegate: cellInfoDelegate! = nil
然后您需要在(cellForRowAt indexPath :)中设置委托(请使用伪代码):
cell.cellInformationDelegate = self
您忘记设置委托,因此未调用委托方法。而且您也没有设置indexPath。因此,您还需要添加:
cell.indexPathInstance = indexPath