0

I have a table view and few custom cells.in one custom cell there is a picker view.I need to load the data to the picker view at cellForRowAtIndexPath

this is my cellForRowAtIndexPath method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifierOne = @"detailCell";
    FlightUserTableViewCell *detailcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierOne];

    NSString *cellIdentifierTwo = @"detailCelltwo";
    FlightUserSecondTableViewCell *detailcelltwo = [tableView dequeueReusableCellWithIdentifier:cellIdentifierTwo];

    if (indexPath.section == 0) {

        if (indexPath.row ==0) {

            //detailcell.titlepickerView **here i want to load the data **

            return detailcell;
        }
        return detailcelltwo;
    }



    return detailcelltwo;
}

how can I do that.I know if we load data to picker view normally we have to implement it's delegate methods like below.

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [cabinArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [cabinArray objectAtIndex:row];
}

so how can I do that for the picker view inside the custom cell.hope your help.

as the answers, I have implemented delegate methods inside the custome tableview cell like this

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    return [leadandadultTitle count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [leadandadultTitle objectAtIndex:row];
}

then inside the cellForRowAtIndexpath I have implemented like this.

 if (indexPath.section == 0) {

        NSString *cellIdentifierOne = @"detailCell";
        FlightUserTableViewCell *detailcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierOne];

        if (indexPath.row ==0) {

            if([traveller isEqualToString:@"LEAD"])
            {

                detailcell.leadandadultTitle = leadandadultTitle;
                detailcell.titlepickerView.delegate = self;
                detailcell.titlepickerView.dataSource = self;
            }
            else
            {
               detailcell.leadandadultTitle = childandinfantTitle;
            }



            return detailcell;
        }

Note :I tried with setting the delegate and datasource like above and also tried dragging the pickerview and selecte delegate and datasource but

numberOfComponentsInPickerView:]: unrecognized selector sent to instance this gives

2
  • Implement the picker delegate protocol in your custom cell and set the data you want the picker to display on cellForRowAtIndexPath: Commented Mar 17, 2016 at 10:44
  • you mean I have to implement the delegate methods inside my cutom cell class, and then how can I load the data at cellForRowAtIndexPath Commented Mar 17, 2016 at 10:48

3 Answers 3

1

Implement the picker delegate protocol in your custom cell and set the data you want the picker to display on cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *cellIdentifierOne = @"detailCell";
  FlightUserTableViewCell *detailcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierOne];

  NSString *cellIdentifierTwo = @"detailCelltwo";
  FlightUserSecondTableViewCell *detailcelltwo = [tableView dequeueReusableCellWithIdentifier:cellIdentifierTwo];

  if (indexPath.section == 0) {
    if (indexPath.row ==0) {
        detailCell.cabinArray = cabinArray
        return detailcell;
    }
    return detailcelltwo;
  }

  return detailcelltwo;
}
Sign up to request clarification or add additional context in comments.

4 Comments

it gives this error :numberOfComponentsInPickerView:]: unrecognized selector sent to instance
the delegate and datasource should be the detailCell not self but that part should be set in the custom cell itself
I dragged the pickerview and selected delegate and datasource then it gives this error
did you set the detailCell as delegate and datasource? Then you have not implemented numberOfComponentsInPickerView on the detailCell
1

This is the full answer for the to implement pickerview inside the custom tableview cell

in cellForRowAtIndexPath method, implement the normal way

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifierOne = @"detailCell";
    FlightUserTableViewCell *detailcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierOne];

    NSString *cellIdentifierTwo = @"detailCelltwo";
    FlightUserSecondTableViewCell *detailcelltwo = [tableView dequeueReusableCellWithIdentifier:cellIdentifierTwo];

    if (indexPath.section == 0) {

        if (indexPath.row ==0) {

            detailcell.leadandadultTitle = leadandadultTitle;
            return detailcell;
        }

        detailcell.leadandadultTitle = otherTitle;
        return detailcelltwo;
    }



    return detailcelltwo;
}

then in your custom tableview cell class implement pickerview delegate methods like normal way

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    return [leadandadultTitle count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [leadandadultTitle objectAtIndex:row];
}

and the important part is, in your custom tableview cell class inside the following method do delegate and datasource like following.

- (void)awakeFromNib {
    // Initialization code
    self.titlepickerView.delegate = self;
    self.titlepickerView.dataSource = self;
}

so this solve the problem to me.special thanx to @Eric his answer helped me lot.

Comments

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *cellIdentifierOne = @"detailCell";
  FlightUserTableViewCell *detailcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierOne];

  NSString *cellIdentifierTwo = @"detailCelltwo";
  FlightUserSecondTableViewCell *detailcelltwo = [tableView dequeueReusableCellWithIdentifier:cellIdentifierTwo];

  if (indexPath.section == 0) {
    if (indexPath.row ==0) {
       UIPickerView *pickerView=(UIPickerView *)[detailCell viewWithTag:12]; //  12 replace value by tag of your pickerView tag in cell 

        pickerView.delegate = self;
        pickerView.dataSource = self;
        return detailcell;
    }
    return detailcelltwo;
  }

  return detailcelltwo;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.