【发布时间】:2019-04-14 14:36:01
【问题描述】:
我有这个使用表格视图的应用程序,并且每个表格视图单元格、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)
}
}
【问题讨论】:
-
Delegate 必须是一个弱属性。改让cellInformationDelegate:cellInfoDelegate!到弱 var cellInformationDelegate: cellInfoDelegate?此外,您忘记设置委托属性。之后 let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardCell") 为! DashboardCell 添加单元格。 cellInformationDelegate = self