在UITableview中添加UIPickerView

时间:2015-06-01 07:02:25

标签: ios xcode uitableview uipickerview

如何在UITableViewCell中添加UIPickerView?

InlinePicker 还有新功能,或者只是尝试在单元格中添加为子视图吗?

请回答我的问题&消除我对inLinePicker的疑惑/怀疑

2 个答案:

答案 0 :(得分:1)

Apple已提供DateCell

的示例代码

以下是它的一些代码片段:

- (BOOL)hasPickerForIndexPath:(NSIndexPath *)indexPath
{
    BOOL hasDatePicker = NO;
    NSInteger targetedRow = indexPath.row;
    targetedRow++;
    UITableViewCell *checkDatePickerCell =
        [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:targetedRow inSection:0]];
    UIDatePicker *checkDatePicker = (UIDatePicker *)[checkDatePickerCell viewWithTag:kDatePickerTag];
    hasDatePicker = (checkDatePicker != nil);
    return hasDatePicker;
}

- (void)updateDatePicker
{
    if (self.datePickerIndexPath != nil)
    {
        UITableViewCell *associatedDatePickerCell = [self.tableView cellForRowAtIndexPath:self.datePickerIndexPath];
        UIDatePicker *targetedDatePicker = (UIDatePicker *)[associatedDatePickerCell viewWithTag:kDatePickerTag];
        if (targetedDatePicker != nil)
        {
            NSDictionary *itemData = self.dataArray[self.datePickerIndexPath.row - 1];

            [targetedDatePicker setDate:[itemData valueForKey:kDateKey] animated:NO];

        }

    }

}
- (BOOL)hasInlineDatePicker

{
    return (self.datePickerIndexPath != nil);
}

- (BOOL)indexPathHasPicker:(NSIndexPath *)indexPath

{
    return ([self hasInlineDatePicker] && self.datePickerIndexPath.row == indexPath.row);

}

- (BOOL)indexPathHasDate:(NSIndexPath *)indexPath

{
    BOOL hasDate = NO;
    if ((indexPath.row == kDateStartRow) ||

        (indexPath.row == kDateEndRow || ([self hasInlineDatePicker] && (indexPath.row == kDateEndRow + 1))))

    {
        hasDate = YES;

    }
    return hasDate;
}

答案 1 :(得分:0)

来自This link

您必须在cellForRowAtIndexPath

中分配UIPickerView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if(indexPath.row == pickerRow){
UIPickerView *pickerView = [[UIPickerView alloc]init];
cell = ... // alloc and initialize a cell
cell addSubview:pickerView];
}
else{ // your other cells }   

return cell;
}