- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用自定义 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!)")
}
我确信 row
和 section
是正确的!我也用 @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/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!