简体   繁体   English

UITableView中的内联UiPickerView

[英]inline UiPickerView in UITableView

I am using more than two inline picker views (Not UIDatePickerView) and it's working fine with all transition but what my problem is its mixing the data sources value and shows both mixed value. 我使用了两个以上的内联选择器视图(不是UIDatePickerView),并且在所有过渡中都可以正常工作,但是我的问题是它混合了数据源值并显示了两个混合值。

I have just used Apple's sample for an inline datepicker for checking. 我刚刚使用Apple的示例进行了内联日期选择器检查。

I have data1PickerView with its own Two DataSource named as data1 and data2 . 我有data1PickerView及其自己的两个DataSource命名为data1data2

My code is as follows when I open the pickerView on tableViewCell didSelect 当我在tableViewCell didSelect上打开pickerView时,我的代码如下

data1=[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil];
data2=[NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",nil];

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


    if(indexPath.row==1){
        isData1=TRUE;
        isData2=FALSE;
    }

    else if(indexPath.row==2){

        isData2=TRUE;
        isData1=FALSE;
    }
    if (cell.reuseIdentifier == kDateCellID)
    {
        if (EMBEDDED_DATE_PICKER)
            [self displayInlineDatePickerForRowAtIndexPath:indexPath];
        else
            [self displayExternalDatePickerForRowAtIndexPath:indexPath];
    }
    else
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}
/*! Reveals the date picker inline for the given indexPath, called by "didSelectRowAtIndexPath".

 @param indexPath The indexPath to reveal the UIDatePicker.
 */
- (void)displayInlineDatePickerForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // display the date picker inline with the table content
    [self.tableView beginUpdates];

    BOOL before = NO;   // indicates if the date picker is below "indexPath", help us determine which row to reveal
    if ([self hasInlineDatePicker])
    {
        before = self.datePickerIndexPath.row < indexPath.row;
    }

    BOOL sameCellClicked = (self.datePickerIndexPath.row - 1 == indexPath.row);

    // remove any date picker cell if it exists
    if ([self hasInlineDatePicker])
    {
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.datePickerIndexPath.row inSection:0]]
                              withRowAnimation:UITableViewRowAnimationFade];
        self.datePickerIndexPath = nil;
    }

    if (!sameCellClicked)
    {
        // hide the old date picker and display the new one
        NSInteger rowToReveal = (before ? indexPath.row - 1 : indexPath.row);
        NSIndexPath *indexPathToReveal = [NSIndexPath indexPathForRow:rowToReveal inSection:0];

        [self toggleDatePickerForSelectedIndexPath:indexPathToReveal];
        self.datePickerIndexPath = [NSIndexPath indexPathForRow:indexPathToReveal.row + 1 inSection:0];
    }

    // always deselect the row containing the start or end date
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    [self.tableView endUpdates];

    // inform our date picker of the current date to match the current cell
    [self updateDatePicker];
}


// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    if(isData1)
    return data1.count;
    else if(isData2)
        return data2.count;
    else
        return 0;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    if(isData1)
        return [data1 objectAtIndex:row];
    else if(isData2)
        return [data2 objectAtIndex:row]
        ;
else
    return @"";
}

- (void)toggleDatePickerForSelectedIndexPath:(NSIndexPath *)indexPath
{
    [tblRegister beginUpdates];

    NSArray *indexPaths = @[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]];

    // check if 'indexPath' has an attached date picker below it
    if ([self hasPickerForIndexPath:indexPath])
    {
        // found a picker below it, so remove it
        [tblRegister deleteRowsAtIndexPaths:indexPaths
                              withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {

        // didn't find a picker below it, so we should insert it
        [tblRegister insertRowsAtIndexPaths:indexPaths
                              withRowAnimation:UITableViewRowAnimationFade];
    }

    [tblRegister endUpdates];
}

can any one please tell me what cause that my data1 datasource shwoing values with data2 Datasource and vice a versa as per following image 谁能告诉我是什么原因导致我的data1数据源与data2数据源交换值,反之亦然,如下图所示

在此处输入图片说明

在此处输入图片说明

What I suspect is that the value of your isData1 and isData2 booleans are not what you expect them to be when the UI is rendering those picker views. 我怀疑的是,当UI呈现这些选择器视图时, isData1isData2布尔值不是您期望的值。 I expect that if you place a breakpoint on your - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component method you will see something awry. 我希望如果在- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component方法上放置一个断点,您会发现有些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM