【发布时间】:2018-06-05 06:03:46
【问题描述】:
我有一个带有 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()
}
}
所有到单元格的 Outlets 都在自定义 UITableViewCell 中定义:
class ItemTableViewCell: UITableViewCell {
@IBOutlet var itemNameTextView: UITextView!
@IBOutlet var itemStateButton: UIButton!
@IBOutlet var addItemButton: UIButton!
@IBOutlet var addTextView: UITextView!
如何从主 ViewController 访问 ItemTableViewCell 中的 outlets 变量的值(读取和更改)?
例如,当在第二个单元格中按下“+”按钮时,我需要将TextView变成FirstResponder,然后保存TextFiled的值。
我还需要编辑 0 节单元格上的 TextFields,然后从中保存数据。
【问题讨论】:
-
试试Eureka。
标签: ios swift uitableview