Search code examples
xcodeuitableviewuipickerview

UIPickerView + TableViewCell + delegates and datasource


I have Table view Controller and separate class which handles for me tableviewcell. Inside the tableview cell I have pickerview. How to implement delegate and datasource for pickerview which is in tableCell class but my delegate functions in tableview controller?


Solution

  • You could have your tableView controller set a property on the tableview cells as they are created indicating that it is the delegate and datasource.

    On the tableviewcell class you created just add a property that is an instance of you tableview controller. Like

    @property (nonatomic, retain) MyTableViewController * pickerDelegate;
    

    Then in your cellForRowAtIndexPath you can set that property to self

    cell.pickerDelegate = self;
    

    You might need to also set some sort extra property like a tag to distinguish between each cell. I would think another property on the tableviewcell like an NSIndexPath would do.