0

I'm searching for a hint/best practice of how to do the following behaviour. I have a UITableView that contains some sections, every section contains custom UITableViewCells. Example: Section 1 contains a UITableViewCell with a UILabel, section 2 contains one UITableViewCell that contains some UITextFields. When I select a UITableViewCell from the section 1 (a UILabel), I get an Object. Does anyone know how to fill the UITextFields with the variables of the selected object from the section 1?

Here is an example use case: I have a UITableView contains 2 sections, In section one I have list of student names, in section 2 I have one custom UITableViewCell that contains some UITextFields, when I select a name from section 1, I want to get the details of that student in the UITextFields.

Cal
  • 422
  • 6
  • 20
Walid
  • 700
  • 2
  • 10
  • 29
  • so in your data source add value for textfeild when user click in first section cell and Reload the tableview. – Gagan_iOS Dec 21 '18 at 08:07
  • check this [link](https://stackoverflow.com/questions/48739894/how-to-get-indexpath-when-image-inside-cell-tapped/48740036#48740036) to access selected row data from tableview, then add that data in your data source of section 2 for fill info in textfield. – Pratik Prajapati Dec 21 '18 at 08:16
  • Its all is the logical part and its lengthy, so first tell us what you have done so far? – dahiya_boy Dec 21 '18 at 08:16
  • @dahiya_boy for now I just select the cell from the section 1 and I get the object, just I want to update the textfields of the cell in the section 2 with the selected object. – Walid Dec 21 '18 at 09:25
  • @Gagan_iOS can you explain me more, please ? – Walid Dec 21 '18 at 09:25
  • Show us your data structure. – dahiya_boy Dec 21 '18 at 09:35
  • show me your code for tableview.. will try to explain on your code. – Gagan_iOS Dec 21 '18 at 11:02

1 Answers1

0

You have to use Notification

in TableViewController:
public let StudentSelectedNotification = Notification.Name(rawValue: "StudentSelectedNotification")
in didSelectRowAt indexPath:
NotificationCenter.default.post(Notification.init(name: StudentSelectedNotification, object: MyObject))
in viewDidLoad:
NotificationCenter.default.addObserver(self, selector: #selector(self.fillTextField(_:)), name: StudentSelectedNotification, object: nil)
in TableViewController:
func fillTextField(_ notification: Notification) { let myObject = notification.object // set the text of textfield here }

Ike
  • 9
  • 5