caldera.sac
caldera.sac

Reputation: 5088

how to load data to picker view inside custom tableview cell in ios, objective c

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

Upvotes: 0

Views: 1395

Answers (3)

caldera.sac
caldera.sac

Reputation: 5088

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.

Upvotes: 1

Yagnesh Dobariya
Yagnesh Dobariya

Reputation: 2251

- (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;
}

Upvotes: 0

Erik Johansson
Erik Johansson

Reputation: 1248

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;
}

Upvotes: 1

Related Questions