【发布时间】:2019-06-16 15:01:24
【问题描述】:
我正在尝试实现类似于 Apple 提醒应用程序的功能,其中一个 tableview 保存所有提醒,最后一个 + 按钮添加一个新对象。
我的对象保存在一个名为 tempActions 的数组中,它是 tableView 的数据源。
按“添加操作”会将一个新对象附加到标题为“空单元格”的数组中。
标题是UITextView,用户可以对其进行编辑,但我不知道该怎么做:
如何从该特定单元格的UITextView 中获取文本,将其附加到正确索引处的数组(索引对应于 indexPath.row),然后将其显示在 cell.label 中?
我想过使用 textViewDidEndEditing 方法,但我不知道如何从 cellForRowAt 方法中引用正确的单元格。
有人能帮忙澄清一下吗,还是我以错误的方式处理它?
这是整个类的代码:
class Step3: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
// Outlets
@IBOutlet weak var sectionText: UILabel!
@IBOutlet weak var sectionHeader: UILabel!
@IBOutlet weak var teableViewHeight: NSLayoutConstraint!
@IBOutlet weak var tableview: UITableView!
@IBAction func addAction(_ sender: Any) {
tempActions.append(Action(title: "Empty Cell", completed: false))
tableview.reloadData()
tableview.layoutIfNeeded()
teableViewHeight.constant = tableview.contentSize.height
print(tempActions)
}
@IBAction func nextAction(_ sender: Any) {
let newGoal = Goal(
title: tempTitle,
description: tempDescription,
duration: tempDuration,
actions: nil,
completed: false
)
newGoal.save()
performSegue(withIdentifier: "ToHome", sender: nil)
}
func textViewDidEndEditing(_ textView: UITextView) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tempActions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ActionCell", for: indexPath) as! ActionCell
cell.label.text = tempActions[indexPath.row].title
cell.label.textContainerInset = UIEdgeInsets(top: 12, left: 0, bottom: 12, right: 0);
cell.label.delegate = self
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableview.estimatedRowHeight = 40
tableview.rowHeight = UITableView.automaticDimension
}
}
提前致谢
【问题讨论】:
-
所以你想点击单元格写一些东西然后重新加载并查看文本作为标题?
-
@NickStefanidis 是的,没错。
标签: swift uitableview custom-cell