如何使用Swift将接收到的值从Viewcontroller B显示到Viewcontroller A tableview自定义单元格textview?
在我的场景中,我将值从如何使用Swift将接收到的值从Viewcontroller B显示到Viewcontroller A tableview自定义单元格textview?,swift,uiviewcontroller,Swift,Uiviewcontroller,在我的场景中,我将值从Viewcontroller B传递到Viewcontroller A。在Viewcontroller A中,我使用CustomCelltextview维护tableview(我只使用了一行)。在这个文本视图中,我需要显示从Viewcontroller B收到的值。下面是我正在使用的代码。请为我的场景提供一些想法 class ViewcontrollerB: UIViewController, isAbleToReceiveData { func pass(da
Viewcontroller B
传递到Viewcontroller A
。在Viewcontroller A
中,我使用CustomCell
textview
维护tableview(我只使用了一行)。在这个文本视图中,我需要显示从Viewcontroller B
收到的值。下面是我正在使用的代码。请为我的场景提供一些想法
class ViewcontrollerB: UIViewController, isAbleToReceiveData {
func pass(data: String) {
print("USER: \(data)")
}
....
....
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
// From TextView Placeholder and Color
cell.from_textview.text = "Enter your text" // here I need to show received “data” from VC_B.
cell.from_textview.textColor = UIColor.lightGray
return cell
}
}
创建一个可以从
VC\u a
var stringReceived: String? = nil
现在在函数中
func pass(data: String) {
print("USER: \(data)")
//add the following
stringReceived = data
self.UITableView_name.reloadData()
}
在func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell
将以下内容从cell.from\u textview.text=“输入您的文本”
更改为cell.from\u textview.text=stringReceived代码>
因此,基本上使用reloadData()
在收到数据后刷新UITableView
func pass(data: String) {
let indexPath = IndexPath.init(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as CustomCell
cell.from_textview.text = data
}
因为您只有一个单元格,所以它将是第0行第0节,并且tableView.reloadData()
也可以使用。但是,如果您有多个单元格,并且希望更改一个特定的单元格,那么最好只调用该单元格,甚至调用self.tableView.reloadRows(位于:[IndexPath],带有:UITableViewRowAnimation)
VC A和VC B之间的关系是什么?现在的B是现在的B还是B现在的A?A现在的B然后B通过将值传递给@SweeperCan我可以得到依赖注入的示例@iOSerAssign默认值到stringReceived
,否则应用程序将崩溃。stringReceived不应该让也不允许,因为你不能在不详细说明的情况下更改该值,依赖项注入将允许您使用类似这样的init方法:init(withData:String)
,然后您可以使用withData
变量将值存储到stringReceived
变量中。剩下的部分将和现在差不多。但是意识到A是在B之前创建的,我认为依赖注入不适合这里的目的!谢谢@dahiya_男孩谢谢你抓住了车祸!答案已编辑!运行时,您无法确定是否使用静态索引XPath。所以,请添加OP如何在pass(数据:)
中获取indexpath,否则答案将不正确或不完整。