如何使用 Swift 显示从 Viewcontroller B 到 Viewcontroller A tableview 自定义单元格文本视图的接收值?
How to show received values from Viewcontroller B to Viewcontroller A tableview custom cell textview using Swift?
我的方案是将值从 Viewcontroller B
传递到 Viewcontroller A
。在 Viewcontroller A
中,我使用 CustomCell
textview
维护表视图(我只使用了一行)。在此文本视图中,我需要显示我从 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_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 indexPath: IndexPath) -> UITableViewCell
将以下内容从 cell.from_textview.text = "Enter your text"
更改为 cell.from_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
}
由于您只有 1 个单元格,因此它将位于第 0 行第 0 节,tableView.reloadData()
也可以。但是如果你有多个单元格并且你想更改 1 个特定的单元格最好只调用那个单元格,甚至 self.tableView.reloadRows(at: [IndexPath], with: UITableViewRowAnimation)
我的方案是将值从 Viewcontroller B
传递到 Viewcontroller A
。在 Viewcontroller A
中,我使用 CustomCell
textview
维护表视图(我只使用了一行)。在此文本视图中,我需要显示我从 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_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 indexPath: IndexPath) -> UITableViewCell
将以下内容从 cell.from_textview.text = "Enter your text"
更改为 cell.from_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
}
由于您只有 1 个单元格,因此它将位于第 0 行第 0 节,tableView.reloadData()
也可以。但是如果你有多个单元格并且你想更改 1 个特定的单元格最好只调用那个单元格,甚至 self.tableView.reloadRows(at: [IndexPath], with: UITableViewRowAnimation)