0

在我看来,我正面临一个独特的问题,为此我参考了以下链接 链接 1链接 2 已经,但我无法完全解决我的问题,所以在这里发布我的查询。我正在使用 11 种不同类型的原型单元实现具有 40 行的 UITableview。其中一个原型单元有一个 UILabel 和一个 UITextfield。现在,由于我正在使用标识符重用此原型单元格,因此我想做以下事情:对于单击文本字段时的第 0 行,我想显示一个日期选择器视图。对于单击文本字段的第 1 行,我想做正常的编辑。对于单击文本字段的第 2 行,我想做正常的编辑。对于单击文本字段的第 3 行,我想显示一个选择器视图。并向下行类似的模式,以重复特定行。我的问题在于如何获取有关委托从哪一行传递的信息。这是我的代码:

if (0 == indexPath.row)
    {
            cellIdentifier = @"CellLabelText";
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            cellLabel = (UILabel *)[cell viewWithTag:101];
            cellLabel.text = [self.modelDataObj.labelTextArray objectAtIndex:indexPath.row];
            textField1 = (UITextField *)[cell viewWithTag:102];
            rowNum = indexPath.row;
            textField1.delegate = self;
            /*  
             placeholder text value is current date set (TBD)
             */

        }
        else if ((1 == indexPath.row) ||(2 == indexPath.row)||(3 == indexPath.row)||
                 (4 == indexPath.row) ||(6 == indexPath.row)||(10 == indexPath.row)||
                 (11 == indexPath.row)||(28 == indexPath.row)||(41 == indexPath.row))
        {
            cellIdentifier = @"CellLabelText";
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            cellLabel = (UILabel *)[cell viewWithTag:101];
            cellLabel.text = [self.modelDataObj.labelTextArray objectAtIndex:indexPath.row];
            rowNum = indexPath.row;
            switch (indexPath.row) {
                case 1:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                case 2:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                case 3:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                case 4:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                case 6:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                case 10:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                case 11:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                case 41:

                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;

                case 28:
                            textField1 = (UITextField *)[cell viewWithTag:102];
                            textField1.delegate = self;
                            break;
                default:
                    break;
            }
        }

这是我的 textShouldBeginEditing 方法代码:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    NSLog(@"In textFieldShouldBeginEditing");
    BOOL finalReturn;
    NSLog(@"row selected is %d", rowNum);
    UIViewController *vc = [[UIViewController alloc] init];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:vc];

    if (0 == rowNum)//)
    {
        UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(5, 5, 300, 600)];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.hidden = NO;
        datePicker.date = [NSDate date];
        [datePicker addTarget:self
                    action:@selector(changeDateInLabel:)
                    forControlEvents:UIControlEventValueChanged];

        [self.view addSubview:datePicker];

        UIBarButtonItem *donebtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPopoverAnimated:)];

        vc.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc] init];
        [vc.navigationItem setRightBarButtonItem:donebtn animated:NO];
        [vc.view setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
        [vc.view addSubview:datePicker];

        self.popvc = [[UIPopoverController alloc] initWithContentViewController:navc];

        [self.popvc setPopoverContentSize:CGSizeMake(datePicker.bounds.size.width+(datePicker.frame.origin.y*2),250+(datePicker.frame.origin.x*2))];
            //NSLog(@"Height of the picker view while embedding in popover %f", pickerView.bounds.size.height);

        [self.popvc presentPopoverFromRect:textField.frame inView:textField.superview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

        finalReturn = NO;
    }
    else if ((1 == rowNum) || (2 == rowNum))
    {
        finalReturn = YES;
    }
    else
    {
    finalReturn = YES;
    }
return finalReturn;
}

问题是 rowNum 设置为当前可见且执行语句的行的值。因此,如果最后一个分配 rowNum 的语句发生在第 12 行,我将无法使用它为 textFieldShouldBeginEditing 中的第 0 行触发一个日期选择器,因为当它设置为 0 时,文本字段没有被点击,当它被点击时 rowNum设置为12左右。基本上我如何为第 0 行触发日期选择器,为第 3 行触发 uipicker,为第 1 行和第 2 行进行正常编辑等等。我无法弄清任何东西,我仍在尝试各种角度。任何帮助都深表感谢。

4

1 回答 1

0

在textFieldShouldBeginEditing中,尝试获取textField的superview,也就是table view cell的content view,得到它的superview,应该是table view cell。

之后,您可以找到该特定表格视图单元格的 indexPath,并根据 indexPath 的行决定要显示的内容。

于 2013-05-28T04:55:14.043 回答