如何将数据加载到 ios、objective c 中自定义 tableview 单元格内的选择器视图

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

我有一个 table 视图和几个自定义 cells.in 一个自定义单元格 有一个选择器 view.I 需要将数据加载到位于 cellForRowAtIndexPath[ 的选择器视图=25=]

这是我的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.titlepickerView **here i want to load the data **

            return detailcell;
        }
        return detailcelltwo;
    }



    return detailcelltwo;
}

我该怎么做 that.I 知道如果我们正常加载数据到选择器视图,我们必须实现它的委托方法,如下所示。

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

所以我怎样才能为自定义中的选择器视图做到这一点 cell.hope 你的帮助。

作为答案,我已经在客户内部实现了 delegate 方法 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];
}

然后在 cellForRowAtIndexpath 里面,我是这样实现的。

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

注意:我尝试像上面那样设置 delegatedatasource,还尝试拖动 pickerview 并选择 delegatedatasource

numberOfComponentsInPickerView:]: unrecognized selector sent to instance 这给出了

在您的自定义单元格中实施选择器委托协议并设置您希望选择器在 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;
}
- (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;
}

这是在 custom tableview cell

中实现 pickerview 的完整答案

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.leadandadultTitle = leadandadultTitle;
            return detailcell;
        }

        detailcell.leadandadultTitle = otherTitle;
        return detailcelltwo;
    }



    return detailcelltwo;
}

然后在您的自定义表格视图单元格中 class 像正常方式一样实施 pickerview 委托方法

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

重要的部分是,在您的自定义表格视图单元格 class 中,在以下方法中执行 delegatedatasource,如下所示。

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

所以这解决了问题 me.special 感谢@Eric 他的回答对我帮助很大。