0

I am facing, in my opinion, a unique issue, for which i have referred following links link 1 & link 2 already but i have not been able to completely solve my issue with those, so posting my query here. I am implementing a UITableview with 40 rows using 11 different types of prototype cells. One of the prototype cell is having a UILabel and a UITextfield. Now since i am reusing this prototype cell using an identifier, i want to do following: for row 0 on click of textfield i want to show a datepicker view. for row 1 on click of textfield i want to do normal editing. for row 2 on click of textfield i want to do normal editing. for row 3 on click of textfield i want to show a picker view. and down the line similar pattern to be repeated for specific rows. My problem lies in the fact as to how to get information as to from which row the delegate was passed on. here is my code:

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

here is my textShouldBeginEditing method code:

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

problem is rowNum is set to value for the row which is currently visible and for which satement was executed. So if the last statement for assignment of rowNum happens during the 12th row, i wouldnt be able to use to it fire a datepicker for row 0 in textFieldShouldBeginEditing, since when it was set to 0, textfield was not clicked and when it was clicked rowNum was set to 12 or so. Basically how do i fire a datepicker for row 0, uipicker for row 3, normal editing for row 1 & 2 and so on so forth. I am not able to make out nething, and i am still trying various angles on it. Any help is deeply appreciated.

Community
  • 1
  • 1
ggthedev
  • 47
  • 8

1 Answers1

0

In the textFieldShouldBeginEditing, try to get the superview of the textField, which is the content view of table view cell, and get its superview,, which should be a table view cell.

After that, you can find the indexPath of that particular table view cell and decide what to show based on the row of indexPath.

Valent Richie
  • 5,226
  • 1
  • 20
  • 21
  • thanks for the pointer and it did light me up for a while, when i tried executing it, but result is not as desirable. to explain more, for prototype cell 0(label, text) its used in rows 0-5. now when i try to get superview of the textfield in textFieldShouldBeginEditing i get a uitableviewcell which is of prototype 1, and then following which when i try to get the indexpath using following code ` UITableViewCell *cell = (UITableViewCell*)[textField superview]; NSIndexPath *index =(NSIndexPath*)[self.tableView indexPathForCell:cell]; ` – ggthedev May 28 '13 at 06:03
  • always get row 0 since indexpath.row always return 0 even if the textfield for row 2 is tapped...this comment is a continuation from the previous comment, sorry for the break since i wasnt able to write whole explanation in 1 comment... – ggthedev May 28 '13 at 06:05
  • @experimentalist Seems you need to use textField.superview.superview to get the cell. – Valent Richie May 28 '13 at 06:22
  • wont that give me a tableview, since superview of textfield would be tableviewcell while its superview would be tableview itself? correct me if i am wrong on this. – ggthedev May 28 '13 at 06:40
  • @experimentalist the structure is table view cell > content view > text field – Valent Richie May 28 '13 at 06:44