Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在另一个函数(Swift)中引用自定义UITableView单元格_Swift_Uitableview_Custom Cell - Fatal编程技术网

如何在另一个函数(Swift)中引用自定义UITableView单元格

如何在另一个函数(Swift)中引用自定义UITableView单元格,swift,uitableview,custom-cell,Swift,Uitableview,Custom Cell,我正在尝试实现类似于苹果的提醒应用程序的功能,在这个应用程序中,一个tableview保存所有提醒,并且在最后一个+按钮添加一个新对象 我的对象保存在一个名为tempActions的数组中,该数组是tableView的数据源 按“添加操作”可将标题为“空单元格”的新对象追加到数组中 标题是一个用户可以编辑的UITextView,但我不知道该怎么做: 如何从该特定单元格的UITextView中获取文本,将其附加到正确索引处的数组中(该索引对应于indexPath.row),然后将其显示在cell.

我正在尝试实现类似于苹果的提醒应用程序的功能,在这个应用程序中,一个tableview保存所有提醒,并且在最后一个+按钮添加一个新对象

我的对象保存在一个名为
tempActions
的数组中,该数组是tableView的数据源

按“添加操作”可将标题为“空单元格”的新对象追加到数组中

标题是一个用户可以编辑的
UITextView
,但我不知道该怎么做:

如何从该特定单元格的
UITextView
中获取文本,将其附加到正确索引处的数组中(该索引对应于indexPath.row),然后将其显示在cell.label中?

我曾想过使用TextViewDiEndediting方法,但我不知道如何从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
}

}
如果我理解的话,请提前感谢--textView在一个单元格中,您希望在
textViewDiEndediting
中找到该单元格。如果textfield的superview是单元格,则可以执行以下操作:

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以查找其索引。

因此,您要单击单元格写入内容,然后重新加载,并将文本视为标题?@NickStefanidis Yes,没错。谢谢你,tableView。indexPath(for:cell)正是我所需要的。但是,如何才能在该单元格的UITextView中获取文本?通常我会在Step3类中创建一个出口,但这是一个自定义单元格,因此出口位于自定义单元格的类中。
textView.text
我的错误是,我意识到自定义单元格不是textfield的超级视图,它会自动包装在内容视图中。如果let cell=textView.superview?.superview为?行动细胞,工作了。非常感谢。