[英]Swift3: How to save UITextView data in UITableViewCell
How do you save a UITextView
data in a UITableViewCell
respectively? 如何将
UITextView
数据分别保存在UITableViewCell
? I have two files, one is TableViewController
and descViewController
. 我有两个文件,一个是
TableViewController
和descViewController
。 In TableViewController
, an array of words are stored and each cell is connected to descViewController
, where a UITextView
is set. 在
TableViewController
,存储了一个单词数组,每个单元格都连接到descViewController
,在此处设置了UITextView
。 I want to save the text data in each cell. 我想在每个单元格中保存文本数据。 How am I able to do it?
我该怎么做? The following is my attempt which almost succeeded
以下是我的尝试,几乎成功了
- In my descViewController; -在我的descViewController中;
@IBOutlet weak var myLabel: UILabel!
var passedValue = ""
var passedText = ""
@IBOutlet weak var myTextView: UITextView!
In viewDidLoad Inside descViewController 在viewDidLoad内部descViewController中
myTextView.delegate = self
myTextView.text = passedText
myTextView.font = UIFont(name: "times new roman", size: 15)
myLabel.text = passedValue
- In my TableViewController; -在我的TableViewController中;
func textData() -> String {
if let txt = textViewUserDefaults.object(forKey: "txtData") as? String {
return txt
}
return ""
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow;
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!;
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "desc") as! descViewController
viewController.passedValue = (currentCell?.textLabel?.text)!
viewController.passedText = textData()
let navController = UINavigationController(rootViewController: viewController)
self.present(navController, animated: false , completion: nil)
}
I have created this method. 我已经创建了这种方法。 The destination when each cell is clicked is
descViewController
where I prepared passedText
and passedValue
. 单击每个单元格时的目标是
descViewController
,我在其中准备了passedText
和passedValue
。 This passedValue
is a UIlable
. 此
passedValue
是UIlable
。 This lable is for a String
to be displayed in descViewController
This result is successful. 此标签用于在
descViewController
显示String
。此结果成功。 However, although my text view data is passed successfully, every cell data is going to hold the same value. 但是,尽管我的文本视图数据已成功传递,但每个单元格数据都将保留相同的值。 For example, if I write something like "Today is a sunny day!", save it, and when I click another cell to open
descViewController
, the result is "Today is a sunny day!" 例如,如果我编写类似“今天是晴天!”的内容,请保存它,然后单击另一个单元格以打开
descViewController
,结果将是“今天是晴天!”。 in my text view. 在我的文本视图中。 This is trouble I have right now.
这是我现在遇到的麻烦。 Code I wrote for storing data is in the following;
我为存储数据编写的代码如下所示;
textViewUserDefaults
is a name I set by using UserDefaults.Standard
. textViewUserDefaults
是我通过设置一个名称UserDefaults.Standard
。
textViewUserDefaults.set(self.myTextView.text, forKey: "txtData")
I want to have each cell hold each value. 我想让每个单元格保存每个值。 Could you provide me with code that works successfully?
您能给我提供成功运行的代码吗?
Thank you 谢谢
let YOURARRAYNAME : NSMutableArray = NSMutableArray()
When user enter some text and submit it then add that text in your array 当用户输入一些文本并提交时,然后将该文本添加到您的数组中
YOURARRAYNAME.addObject(YOURTEXT)
In this method 用这种方法
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
You got tableview cell index path so when you want to pass a data 您获得了tableview单元格索引路径,因此当您想要传递数据时
let viewController = storyboard.instantiateViewController(withIdentifier: "desc") as! descViewController
viewController.passedValue = YOURARRAYNAME.objectAtIndex(indexpath.row) // here you pass text and what is passedvalue i dont know so you check this
viewController.passedText = textData() // what is your passedtext and textdata() you also pass value here
let navController = UINavigationController(rootViewController: viewController)
self.present(navController, animated: false , completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.