简体   繁体   English

Swift3:如何在UITableViewCell中保存UITextView数据

[英]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 . 我有两个文件,一个是TableViewControllerdescViewController 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 ,我在其中准备了passedTextpassedValue This passedValue is a UIlable . passedValueUIlable 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.

相关问题 如何在UITextView,Swift3和Parse服务器(Bitnami)上显示Array元素 - How to display Array elements on UITextView, Swift3 & Parse server (bitnami) 如何在 Swift3 中更改 UITextView 中文本字符串的颜色 - How to change color of text strings inside UITextView in Swift3 Swift:如何使UITableViewCell的高度适应其中的UITextView - Swift: how to adapt UITableViewCell height to a UITextView inside it 如何使用userDefaults在Swift3中保存UISegmentedControl的状态 - How to save the state of UISegmentedControl in Swift3 with userDefaults 如何将遮罩应用于保存图像(swift3) - How to apply mask to a save image (swift3) swift3中的其他方法如何更新UITableviewcell标签? - How to update UITableviewcell label in other method in swift3? 火力基地。 如何将数据另存为事务而不覆盖现有数据,而是对其进行更新? 迅捷3 - Firebase. how to save data as transactions without overwriting the existing data, but updating it? Swift3 在UITableViewCell中检测UITextView中的数据 - Detect Data in UITextView in UITableViewCell 如何在swift3中的UILabel中获取JSON数据 - How to get JSON data in UILabel in swift3 将 CMTime 保存在核心数据 + Xcode8 + swift3 - save CMTime in core data + Xcode8 + swift3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM