gpt4 book ai didi

ios - 使用自定义 UITableViewCell 中的按钮获取单元格字符串的问题

转载 作者:行者123 更新时间:2023-11-28 12:34:44 25 4
gpt4 key购买 nike

我正在尝试使用自定义 UITableViewCell 中的按钮获取单元格的 string 。但是当我点击按钮时,应用程序由于此错误而崩溃: fatal error :在展开可选值时意外发现 nil

我尝试了 print 一些东西并且它有效!但是当我尝试获取其他单元格的字符串时,应用程序崩溃了。这是我的代码:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cellID:String!
var cell = OrderCell()

switch indexPath.section {
.
.
.
.
.

case 5 :
if indexPath.row == 0 {cellID = "Submit" }
cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! OrderCell


cell.submitButton.addTarget(self, action: #selector(OrderViewController.submitNewOrder), for: .touchUpInside)

default:
break
}
return cell

}



func submitNewOrder() {
//Title
let index1:IndexPath = IndexPath(row: 0, section: 0)
let cell1: OrderCell = tableView.cellForRow(at: index1) as! OrderCell
print("order is \(cell1.orderTitle.text!)")

}

我确信 rowsection 是正确的!我也用 @IBAction 厌倦了同样的方法,它工作正常!有什么问题?

谢谢

最佳答案

你只需要,

self.tableView(tableView, cellForRowAt: index1)

而不是

tableView.cellForRow(at: index1) as! OrderCell

它是如何工作的??

在表格 View 中,单元格被重用。仅当您滚动 tableview 并稍后点击底部某个单元格中的按钮时,您的代码才会崩溃,原因是索引路径 (0,0) 处的单元格被重用,因此 tableView 不会将单元格保留在索引路径 (0,0) 处,因此当你调用 self.tableView(tableView, cellForRowAt: index1) 它返回 nil。

另一方面,您可能应该做的是要求数据源委托(delegate)返回单元格而不是 tableView 本身。在这种情况下,您自己的VC是数据源,因此我写了

self.tableView(tableView, cellForRowAt: index1)

这将在索引路径 (0,0) 处获取对单元格的引用,即使它不存在于 tableView 中并将其交给你而不是 nil :)

建议:

虽然上面的代码工作得很好,但我个人建议从按钮到单元格使用 IBAction,因为单元格包含按钮,所以在单元格而不是 tableViewController 中创建按钮的 IBAction 在逻辑上是有意义的。

您始终可以在您的单元格中声明一个委托(delegate)并在您的 TableViewController 中实现它以通知按钮点击并让知道点击了哪个按钮。

这种方式的代码对任何看过一次的人来说都是干净的。 Cell 负责处理其 subview 的 IBAction,并仅通知 tableViewController 在点击按钮时要做什么。另一方面,TableViewController 只是执行任务,从不担心它被点击的是哪个按钮。

编辑:

let index1:IndexPath = IndexPath(row: 0, section: 0)
let cell1 = self.tableView(tableView, cellForRowAt: index1) as! OrderCell

我不会更改任何您可以使用现有代码的内容,如上所示 :) 只需替换 cellForRowAt :)

编辑 2:

虽然上面提供的代码工作得很好,但在我们的聊天中(您可以引用评论部分的聊天)我意识到 indexPath (0,0) 处的单元格有一个 textFiled。

虽然上述解决方案适用于标签,但在将其与 TextField 一起使用时,它会将 textField.text 返回为“”。

这显然是因为 cellForRowAtIndexPath 的错误和 UITableView 的单元格重用策略。

虽然我在上面的答案的建议部分提出了正式和更合适的建议,但我相信因为答案被接受,我有责任提供功能齐全的代码,该代码也适用于 textField :)

最简单的做法是,一旦用户在 indexPth (0,0) 单元格的文本字段中输入一些文本,就更新数据源,并在 tableView 的 cellForRowAt 索引路径中正确更新文本字段文本。

正确执行此操作将不再在向下滚动或重复使用单元格时将 textField 文本返回为“”。

这是我的做法:)

第 1 步:

使您的单元格成为 textField 委托(delegate)并实现 textFieldDidEndEditing :)

class MyTableViewCell: UITableViewCell,UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
//will see implementation later
}
}

第 2 步:

在您的自定义单元格中声明一个协议(protocol),以通知 tableViewController 使用用户文本更新其数据源:)

protocol updateDataSource {
func updateDataSource(with userText : String)
}

第 3 步:

在单元格中创建一个委托(delegate) :)

var delegate : updateDataSource? = nil

第 4 步:

实现 textFieldDidEndEditing,

func textFieldDidEndEditing(_ textField: UITextField) {
self.delegate?.updateDataSource(with: textField.text!)
}

第 5 步:

在您的 ViewController (TableViewController) 中确认委托(delegate)

extension ViewController : updateDataSource {
func updateDataSource(with userText: String) {
//update your data source
//I dont have any hence am updating a instance variable in my VC
self.userEnterredText = userText
}
}

最后将您的 CellForRowAtIndexPath 更新为

        cell.delegate = self
if indexPath.row == 0 && self.userEnterredText != nil {
cell.testTextField.text = self.userEnterredText!
}
else {
cell.testTextField.text = ""
}
return cell

这将确保当您在重用单元格的数据源上调用 self.tableView(tableView, cellForRowAt: index1) 时,在索引路径处为行调用单元格,并且因为您在 cellForRowAt 中的代码现在将正确填充您的文本字段:)

现在您仍然可以使用您的代码

let index1:IndexPath = IndexPath(row: 0, section: 0)
let cell1: OrderCell = tableView.cellForRow(at: index1) as! OrderCell
print("order is \(cell1.orderTitle.text!)")

还有 textField :)

希望对您有所帮助:)

关于ios - 使用自定义 UITableViewCell 中的按钮获取单元格字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com