【问题标题】:How to reference custom UITableView cell inside another function (Swift)如何在另一个函数中引用自定义 UITableView 单元格(Swift)
【发布时间】: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


【解决方案1】:

如果我理解了——textView 在一个单元格中,而您想在textViewDidEndEditing 中找到那个单元格。如果文本字段的超级视图是单元格,您可以这样做:

func textViewDidEndEditing(_ textView: UITextView) {
    if let cell = textView.superview as? ActionCell, 
         let indexPath = tableView.indexPath(for: cell) {
      // Now you have the indexPath of the cell
      // update tempActions

           // YOUR CODE HERE

      // Then reloadRows
      tableView.reloadRows(at: [indexPath]), with: .automatic)
    }
}

您可以做的另一件事是让 tempAction 的类型具有唯一 ID,然后将其存储在 ActionCell 中——当您要查找索引时,在 tempActions 数组中查找 ID 以找到它的索引。

【讨论】:

  • 谢谢你,tableView.indexPath(for: cell) 正是我需要的。我怎么能得到那个单元格的 UITextView 里面的文本呢?通常我会在我的 Step3 类中创建一个插座,但这是一个自定义单元格,因此插座位于自定义单元格的类中。
  • textView.text
  • 我的错误,我意识到自定义单元格不是文本字段的超级视图,它会自动包装在内容视图中。 if let cell = textView.superview?.superview as? ActionCell,工作。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-04-10
  • 2018-12-19
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 2018-01-06
相关资源
最近更新 更多