[英]How to access custom UITableViewCell from main ViewController
我有一个带有UITableView的主视图控制器。 表视图有2个在IB中原型化的单元。
第一个单元格位于第0节中,第二个单元格位于第1节中(始终位于表格的底部)。
这是我在主要ViewController中的表定义:
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return itemNames.count
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Created additional section where will be the cell for adding items (always at bottom)
switch indexPath.section {
case 0:
let cellIdentifier = "itemCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ItemTableViewCell
cell.itemNameTextView.text = itemNames[indexPath.row]
let currentImage = itemImages[indexPath.row]
cell.itemStateButton.setImage(UIImage(named: currentImage), for: UIControlState.normal)
return cell
case 1:
let cellIdentifier = "addItemCell"
let addCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ItemTableViewCell
addCell.addItemButton.setImage(UIImage(named: "plus-math-40"), for: UIControlState.normal)
return addCell
default:
return UITableViewCell()
}
}
单元格的所有出口都在自定义UITableViewCell中定义:
class ItemTableViewCell: UITableViewCell {
@IBOutlet var itemNameTextView: UITextView!
@IBOutlet var itemStateButton: UIButton!
@IBOutlet var addItemButton: UIButton!
@IBOutlet var addTextView: UITextView!
如何从主ViewController访问ItemTableViewCell中插座变量的值(读取和更改)?
例如,当在第二个单元格中按下“ +”按钮时,我需要让TextView成为FirstResponder,然后保存TextFiled的值。
而且我还需要在0节单元格上编辑TextField,然后从中保存数据。
如果只有2个 部分,每部分一行,那么您可以给addItemButton标签值等于部分编号。 然后将操作添加到该按钮并定义相应的选择器。 在该选择器方法中,从发件人的标签获取部分值,并获取与部分和row(= 0)对应的索引路径。 从该索引路径中获取单元格,然后可以访问主控制器中Cell的任何出口。
谢谢
你想要
UITableViewCell
上获取按钮按下事件 UIViewController
更改IBOutlet值 首先,要获得单元格对象上的按钮按下事件,您有2个选项:
1.1实例化单元格时附加一个目标:
// In your tableView cellForRow method
addCell.addItemButton.addTarget(self, action: #selector(bringUpTextfieldMethod), for: .touchUpInside)
1.2使用委托结构:让ItemTableViewCell
声明委托协议,通过IBAction
插座捕获此类中的按钮按下事件,并使用委托传递事件。 主ViewController符合协议并实现委托回调。
其次-访问单元的IBOutlets。 您需要访问单元的任何地方:
let indexPath = IndexPath.init(row: 0, section: 0) // Obviously with the correct row/sec
let itemCell = tableView.cellForRowAtIndexPath(indexPath) as! ItemTableViewCell // your tableview must be an IBOutlet on your view controller
// Now you can access properties on itemCell
// e.g:
let name = itemCell.itemNameTextView.text
我已经将当前单元格定义为自定义单元格实例(这是在单元格中完成TextView编辑后将数据保存到CoreData的代码):
func textViewDidEndEditing(_ textView: UITextView) {
currentTextView = nil
// Finished edit tex tin text view. Save new values to CoreData
let currentCell = tableView.cellForRow(at: currentIndexPath) as! ItemTableViewCell
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
// Add Mode. Adding values from addItemCell to CoreData
if editMode == false {
// In add mode we need to add a new record to CoreData
// Check if textview is not empty
if currentCell.addTextView.text != "" {
shoppingItem = ItemMO(context: appDelegate.persistentContainer.viewContext)
shoppingItem.itemName = currentCell.addTextView.text
shoppingItem.itemCreatedDate = Date()
shoppingItem.itemDone = false
currentCell.addTextView.text = ""
}
} else {
// In edit mode we need to save current values and save context
if currentCell.itemNameTextView.text != "" {
shoppingItems[currentIndexPath.row].itemName = currentCell.itemNameTextView.text
}
}
appDelegate.saveContext()
tableView.reloadData()
}
}
开始编辑单元格时使用的currentIndexPath值:
func textViewDidBeginEditing(_ textView: UITextView) {
currentTextView = textView
var superview = textView.superview
while let view = superview, !(view is UITableViewCell) {
superview = view.superview
}
guard let cell = superview as? UITableViewCell else {
//print("button is not contained in a table view cell")
return
}
guard let indexPath = tableView.indexPath(for: cell) else {
//print("failed to get index path for cell containing button")
return
}
// Setup current index path value
self.currentIndexPath = indexPath
// Setup edit mode for data saving in textViewDidEndEditing
if currentIndexPath.section == 0 {
editMode = true
} else {
if currentIndexPath.section == 1 {
editMode = false
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.