In case I have 2 classes, Test_TableViewController and TestCell_TableViewCell. This is code in class Test_TableViewController
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TestCell_TableViewCell = tableView.dequeueReusableCellWithIdentifier("celltest", forIndexPath: indexPath) as TestCell_TableViewCell
cell.generalcontent()
}
This is code in class TestCell_TableViewCell
var textarea = UITextView(frame: CGRect(x: 10, y: 10, width: 300, height: 180))
func generalcontent()
{
self.addSubview(textarea)
}
How to get the value in textarea entered by the user in Test_TableViewController class? Thankyou
One class should not read the UI elements of another class. This indicates that you're breaking the Model-View-Controller architecture that Cocoa relies on. There should be a model object that holds the data you want. You can pass that whole object from your view controller to your cell. The cell can update it if needed as the user modifies the text area, and the view controller will be able to see the changes because they are both looking at the same model object.
For an introduction, see Apple's Cocoa Core Competencies: Model-View-Controller .
Make the UITextView as a property of TestCell_TableViewCell. Then you can access the UITextView from the Test_TableViewController by using the TestCell_TableViewCell object.
Edit: You can declare a textview as a variable of the TableViewCell class.
var cellTextView:UITextView
Then you can access the text value by
tableViewCell.cellTextView.text
tableViewCell is an object of your class TestCell_TableViewCell.
How about using an ivar to keep a reference to the textarea?
var textarea:UITextView?
func generalcontent()
{
self.textarea = UITextView(frame: CGRect(x: 10, y: 10, width: 300, height: 180))
self.addSubview(self.textarea)
}
Then in Test_TableViewController
you can get the value by doing:
cell.textarea.value
cellForRowAtIndexPath()
is returning a dynamic cell (as your example is), do not add or remove subviews in it, or in any method that it calls. UITextView
an @IBOutlet
of the cell, and then your table can access it via the cell.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.